[plt-scheme] Permutation correct way to do it ?

From: Rocky (rockysqq at yahoo.com)
Date: Fri May 2 22:56:03 EDT 2008

--- Matthias Felleisen <matthias at ccs.neu.edu> wrote:

> 
> But you're using an accumulator, violating the design recipe of the  
> section in which the problem lives. 60%
> 
> -- Matthias


Grrr.  I remember now why I like imperative style better.  Six
functions!  I had one with "only" five functions that worked if the
letters were unique, but never returned if there were duplicates.
Obviously that was no good for newspaper "Jumble" games.

Now don't tell me I can't hide a 'pea to find my place...

(I gotta get over 60% this time.  This Prof is a hard grader.
Just forget there's no comments. :)
--rocky

(define (arrange aword)
  (cond
    ((empty? aword) (cons empty empty))
    (else (inter/all (first aword) (arrange (rest aword))))))


(define (inter/all elem alist)
  (cond
    ((empty? alist) empty)
    (else
     (inter/one elem
                (cons 'pea (first alist))
                (inter/all elem (rest alist))))))


(define (inter/one elem aword alist)
  (cond
    ((more? aword)
     (cons (pea->elem elem aword)
           (inter/one elem (next aword) alist)))
    (else
     (cons (pea->elem elem aword) alist))))


(define (more? aword)
  (cond
    ((symbol=? 'pea (first aword)) (not (empty? (rest aword))))
    (else (more? (rest aword)))))


(define (next aword)
  (cond
    ((symbol=? 'pea (first aword))
     (cons (first (rest aword))
           (cons 'pea (rest (rest aword)))))
    (else (cons (first aword) (next (rest aword))))))


(define (pea->elem elem aword)
  (cond
    ((symbol=? 'pea (first aword)) (cons elem (rest aword)))
    (else (cons (first aword) (pea->elem elem (rest aword))))))



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


Posted on the users mailing list.