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

From: richard gere (diggerrrrr at gmail.com)
Date: Sat Apr 5 14:47:06 EDT 2008

Hello Matthias , thanks for your fast reply , i posting the code again
, can now be read from top.


>  When you go from templates to definitions, read out the PURPOSE
>  statement for the recursive template expression:
>
>  ;; (arrangements a-word) produces the list of words that can be
>  ;; formed with all the letters in the word (a list of letters)
>
>  (arrangements (rest a-word)) therefore produces the list of words
>  that can be formed from all the letters in the rest of the word.
>  (What does this mean for a concrete example? Use the examples!!)
>
>  And then ask yourself how a primitive or a helper function can
>  use this and the rest of the template (line) to get the real
>  result: the arrangements for all the words.

You have explained it nicely .This is what i did in the function
(insert-everywhere-in-a-word a-sym a-word) , i first laid out the
template , then worked with the examples and from it i added the
helper functions.


Here is the code :

CODE BEGINS

;;final2

;;Data Defination for a word
;;A word is either
;;1. empty or
;;2. (cons s wd) where s is a symbol in 'a,'b,'c,...'z and wd is a word
;;Example
;; (cons 'a (cons 'b (cons 'c empty))) is a word


;;Data defination 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
;;Example
;;(cons (cons 'b (cons 'c empty)) (cons (cons 'c (cons 'b empty)) empty))



;;arrangements : word -> list-of-words
;;to create a list of words  by rearranging the letters in a-word
;;Examples:
;;given the input (cons 'a (cons 'b empty)) -> (cons (cons 'a (cons 'b empty))
;;                                               (cons (cons 'b (cons
'a empty)) empty))
;;given (cons 'a (cons 'b (cons 'c empty))) -> ( '(a b c) '(b a c) '(b
c a) '(a c b) '(c a b) '(c b a) )

(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)
;;Examples : given the input 'b (cons (cons 'c empty) empty) ->
;;   (cons (cons 'b (cons 'c empty)) (cons (cons 'c (cons 'b empty)) empty))
;;given  'c (cons empty empty) -> (cons (cons 'c empty) empty)

(define (insert-everywhere/in-all-words a-sym alow)
  (cond
    [(empty? alow) empty]
    [else (append (insert-everywhere-in-a-word a-sym (first alow))
          (insert-everywhere/in-all-words a-sym (rest alow))) ]))



;;insert-everywhere-in-a-word : symbol word -> list-of-words
;;to create a list of words by inserting a-sym at the beginning ,
;;in between the letters and at the end of a-word.
;;Examples :
;;given the input: 'a (cons 'b (cons 'c empty)) ->
;;(cons (cons 'a (cons 'b (cons 'c empty)))
;;(cons (cons 'b (cons 'a (cons 'c empty)))
;;(cons (cons 'b (cons 'c (cons 'a empty))) empty)))

;;given the input: 'c empty
;;(cons (cons 'c empty) empty)

(define (insert-everywhere-in-a-word a-sym a-word)
  (cond
    [(empty? a-word) (cons (insert-at-begining a-sym a-word) empty)]
    [else (append (cons (insert-at-begining a-sym a-word) empty)
(insert-in-between-letters a-sym a-word)
          (cons (insert-at-end a-sym a-word) empty)) ]))


;;insert-at-begining : symbol word -> word
;;to insert a-sym at the begining of a-word
;;Examples :
;;given 'a (cons 'b (cons 'c empty)) -> (cons 'a (cons 'b (cons 'c empty)))
;;given 'a empty -> (cons 'a empty)
(define (insert-at-begining a-sym a-word)
  (cond
    [(empty? a-word) (cons a-sym empty) ]
    [else (cons a-sym a-word) ]))


;;insert-in-between-letters : symbol word -> list-of-words
;;to create a list-of-words by inserting a-sym between the letters of a-word
;;Examples :
;;given 'a (cons 'b (cons 'c (cons 'd empty))) ->
;;(cons (cons 'b (cons 'a (cons 'c (cons 'd empty))))
;;(cons (cons 'b (cons 'c (cons 'a (cons 'd empty)))) empty))

(define (insert-in-between-letters a-sym a-word)
  (cond
    [(empty? (rest a-word)) empty]
    [else (cons    (cons (first a-word) (cons a-sym (rest a-word)))
                   (insert-at-front (first a-word)
                                    (insert-in-between-letters a-sym
(rest a-word)))) ]))



;;insert-at-front : symbol list-of-words -> list-of-words
;;to create a list-of-words by inserting a-sym at the front of all
;;words in the list-of-words (alow)
;;eg given 'b (cons (cons 'c (cons 'a (cons 'd empty))) empty) ->
;;(cons (cons 'b (cons 'c (cons 'a (cons 'd empty)))) empty)
(define (insert-at-front a-sym alow)
  (cond
    [(empty? alow) empty]
    [else (cons (cons a-sym (first alow))  (insert-at-front a-sym
(rest alow))) ]))


;;insert-at-end : symbol word -> word
;;to insert a-sym at the end of a-word
;;Example : given  'a (cons 'b (cons 'c empty)) -> (cons 'b (cons 'c
(cons 'a empty)))
(define (insert-at-end a-sym a-word)
  (cond
    [(empty? a-word) (cons a-sym empty) ]
    [else (append a-word (cons a-sym empty)) ]))



;;Tests for (insert-at-end a-sym a-word)
(check-expect (insert-at-end 'a (cons 'b (cons 'c empty)))
              (cons 'b (cons 'c (cons 'a empty))))


;;Tests for insert-at-front a-sym alow)
(check-expect (insert-at-front 'b (cons (cons 'c (cons 'a (cons 'd
empty))) empty))
              (cons (cons 'b (cons 'c (cons 'a (cons 'd empty)))) empty))

(check-expect (insert-at-front 'b (cons (cons 'c (cons 'a empty))
                                        (cons (cons 'd (cons 'e empty)) empty)))
              (cons (cons 'b (cons 'c (cons 'a empty)))
                    (cons (cons 'b (cons 'd (cons 'e empty))) empty)))



;;Tests for (insert-in-between-letters a-sym a-word)
(check-expect (insert-in-between-letters 'a (cons 'b (cons 'c (cons 'd empty))))
              (cons (cons 'b (cons 'a (cons 'c (cons 'd empty))))
                   (cons (cons 'b (cons 'c (cons 'a (cons 'd empty)))) empty)))



;;Tests (insert-at-begining a-sym a-word)
(check-expect (insert-at-begining 'a (cons 'b (cons 'c empty)))
              (cons 'a (cons 'b (cons 'c empty))) )
(check-expect (insert-at-begining 'a empty) (cons 'a empty))


;;Tests for (insert-everywhere-in-a-word a-sym a-word)
(check-expect (insert-everywhere-in-a-word 'c empty)
              (cons (cons 'c empty) empty))

(check-expect (insert-everywhere-in-a-word 'a (cons 'b (cons 'c empty)))
              (cons (cons 'a (cons 'b (cons 'c empty)))
                    (cons (cons 'b (cons 'a (cons 'c empty)))
                          (cons (cons 'b (cons 'c (cons 'a empty))) empty))))



;;Tests for (insert-everywhere/in-all-words a-sym alow)
(check-expect (insert-everywhere/in-all-words 'b (cons (cons 'c empty) empty))
              (cons (cons 'b (cons 'c empty)) (cons (cons 'c (cons 'b
empty)) empty)))



;;Tests for (arrangements a-word)
(check-expect (arrangements (cons 'a empty)) (cons (cons 'a empty) empty))

(check-expect (arrangements (cons 'a (cons 'b empty)))
(cons (cons 'a (cons 'b empty))
(cons (cons 'b (cons 'a empty)) empty)))

(check-expect (arrangements (cons 'a (cons 'b (cons 'c empty))))
(cons (cons 'a (cons 'b (cons 'c empty)))
(cons (cons 'b (cons 'a (cons 'c empty)))
(cons (cons 'b (cons 'c (cons 'a empty)))
(cons (cons 'a (cons 'c (cons 'b empty)))
(cons (cons 'c (cons 'a (cons 'b empty)))
(cons (cons 'c (cons 'b (cons 'a empty))) empty))))))    )

(generate-report)

CODE ENDS

Thanks
Veer


Posted on the users mailing list.