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

From: richard gere (diggerrrrr at gmail.com)
Date: Fri Apr 4 05:21:49 EDT 2008

Thank you for taking a look at my code.
I knew that something was not right about my code , thats why i wanted to ask .

I have produces a second iteration of the problem , this time at least
it is better then previous code. In this i have followed the design
recipe closely and have commented on my thought process . Working
through a input examples did help.  Please comment , for me  its
important that when solving a problem i follow the design recipe
closely , any further comment is welcome.

I am a beginner , but i have tinkered around with programming before .
I am learning by doing all the exercise in the book ,leaving none .


--CODE--START--

;;Arrangements of words

;;We start with some data definition for a word and list of words .

;;Data Definition for a word
;;A word is either
;;1. empty or
;;2. (cons a wd) where a is a symbol in 'a,'b,'c,...'z and wd is a word
;;eg (cons 'b empty) is a word ,  (cons 'r (cons 'e (cons 'a (cons 'd
empty) ))) is a word

;;Data definition for a list-of-words
;;A list-of-words is either
;;1. empty or
;;2 (cons wd lw) where wd is a word and lw is a list-of-words
;;eg
;;(cons (cons 'r (cons 'e (cons 'a (cons 'd empty))))
;;(cons (cons 'd (cons 'e (cons 'a (cons 'r empty)))) empty))
;;is a list-of-words with two word in it


;;arrangements : word -> list-of-words
;;to create a list of words  by rearranging the letters in a-word
;;eg (arrangements (cons 'a (cons 'b empty) ) ) will give us
;;(cons (cons 'a (cons 'b empty) ) (cons (cons 'b (cons 'a empty) ) empty ))
(define (arrangements a-word)
 (cond
   [(empty? a-word) (cons empty empty) ]
   [ else (insert-everywhere/in-all-words (first a-word)
(arrangements (rest a-word)) ) ]))


;;insert-everywhere/in-all-words : symbol list-of-words -> list-of-words
;;to create a list of words by inserting a-sym between all letters and
;;at beginning and at end of all words in the list of words (alow)
;;eg (insert-everywhere/in-all-words 'a (cons (cons 'b empty) empty) ) gives us
;;(cons (cons 'a (cons 'b empty)) (cons (cons 'b (cons 'a empty)) empty))
(define (insert-everywhere/in-all-words a-sym alow)
 (cond
   [(empty? alow) empty]
   [else (append (insert-symbol-in-a-word a-sym (first alow) empty)
                 (insert-everywhere/in-all-words a-sym (rest alow))) ]))



;;Somehow we need to get a list-of-words from a word , which mean
processing a-word
;;Since we need to process a word , which is a list , we need another
auxiliary function
;;Since we need to insert symbols in a word , a good candidate for it is
;;insert-symbol-in-a-word.

;;first iteration
;;insert-symbol-in-a-word : a-sym word -> list-of-words
;;to create a list of words by inserting a-sym in a-word
;;eg (insert-symbol-in-a-word 'a (cons 'b empty)) give us
;;(cons (cons 'a (cons 'b empty)) (cons (cons 'b (cons 'a empty)) empty))

;;(define (insert-symbol-in-a-word a-sym a-word)
;;  (cond
;;    [(empty? a-word) empty]
;;    [else (cons (append (cons a-sym empty)  a-word )
(insert-symbol-in-a-word a-sym (rest a-word))) ]))

;;when working with the input: 'a     (cons 'b (cons 'c empty) ) i get
;;1. (cons (cons 'a (cons 'b (cons 'c empty)))
;;2. (cons (cons 'a (cons 'c empty))
;;3. empty)

;; here i tinkered more , but soon i realized that for input 'a ('b
'c) i needed :
;; first case('a 'b 'c) i don't need anything before it i can do: a-sym + a-word
;; second case ('a 'c) i need (first a-word) before its and  a-word
after it (a-word is shrinking)
;; so what i needed was to construct a list from a-word as we go along
starting from empty


;;here is the final definition
;;insert-symbol-in-a-word : a-sym word -> list-of-words
;;to create a list of words by inserting a-sym between cl and
;; a-word for all letters in a-word
;;eg (insert-symbol-in-a-word 'a (cons 'b empty)) give us
;;(cons (cons 'a (cons 'b empty)) (cons (cons 'b (cons 'a empty)) empty))
(define (insert-symbol-in-a-word a-sym a-word cl)
 (cond
   [(empty? a-word) (cons (append cl (list a-sym ) a-word ) empty)]
   [else (cons (append cl (cons a-sym empty)  a-word )
               (insert-symbol-in-a-word a-sym (rest a-word)
                                        (append cl (cons (first
a-word) empty)))) ]))



;;tests
(check-expect (arrangements
              (cons 'a (cons 't empty)))

              (cons (cons 'a (cons 't empty))
              (cons (cons 't (cons 'a empty)) empty)))



(check-expect (arrangements (cons 'a (cons 't (cons 'z empty))))
             (cons (cons 'a (cons 't (cons 'z empty)))
             (cons (cons 't (cons 'a (cons 'z empty)))
             (cons (cons 't (cons 'z (cons 'a empty)))

             (cons (cons 'a (cons 'z (cons 't empty)))
             (cons (cons 'z (cons 'a (cons 't empty)))
             (cons (cons 'z (cons 't (cons 'a empty))) empty)))))))


(generate-report)


---CODE END------



Thanks
Veer


Posted on the users mailing list.