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

From: Rocky (rockysqq at yahoo.com)
Date: Wed Apr 30 05:22:35 EDT 2008

Matthias said:
>                                     This exercise is within reach
>for someone who has never programmed before and follows the recipe;
>if you fall in this group, systematically use the recipe and you will
>get there. It is a stumbling block for people who have programmed
>before and believe that I made up the design recipe to torture them.

It's so simple, so very simple, that only a child can do it!

Look Ma, no append ...

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

(define (inter/all elem alist)
    ((empty? alist) empty)
     (inter/one empty
                (first alist)
                (inter/all elem (rest alist))))))

(define (inter/one worms elem can alist)
    ((empty? can) (cons (stuff worms (cons elem empty)) alist))
     (cons (stuff worms (cons elem can))
           (inter/one (cons (first can) worms)
                      (rest can)

(define (stuff worms can)
    ((empty? worms) can)
    (else (stuff (rest worms) (cons (first worms) can)))))

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.