[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 ...
--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 empty
                elem
                (first alist)
                (inter/all elem (rest alist))))))


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


(define (stuff worms can)
  (cond
    ((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.