Hey guys,

I just completed exercise 12.4.2 and would like to know whether I totally
abused the design recipe or not.

It does give the correct results but I have a feeling that I over
complicated it.

Beginning of program:

; a word is either
;;1. empty, or
;;2. (cons s w) where s is a symbol ('a..'z) and w is a word

;;example (cons 'a (cons 'b empty))

;;Template word
;;(define (fun-word a-word)
;;  (cond
;;   [(empty? a-word)...]
;;    [else
;;     (first a-word) (fun-word (rest a-word))]))

;; a list of word is either
;;1. empty, or
;;2. (cons w low) , where w is a word, and low is a list of words

;;ex (cons (cons 'a (cons 'b empty)) (cons (cons 'c (cons 'd  empty ))
empty))

;;Template low
;;(define (fun-low a-low)
;;  (cond
;;    [(empty? a-low)...]
;;    [else
;;     (first a-low) (fun-low (rest a-low))]))

;;arangements : word -> list-of-words
;;to create a list of all rearrangements of the letters in a-word
(define (arrangements a-word)
  (cond
    [(empty? a-word) (cons empty empty)]
    [else
     (insert-everywhere (first a-word) (arrangements (rest a-word)))]))


;;insert-everywhere : symbol low : low
;;inserts a given letter into every element between all possible letters and
;;at beginning and end
(define (insert-everywhere letter a-low)
  (cond
    [(empty? a-low) empty ]
    [else
     (append (create-com-word letter (first a-low) (how-many-combinations
(first a-low)))
             (insert-everywhere letter (rest a-low)))]))

;;create-com-word : symbol word N -> low
;;creates a list of all the possible unique combinations(natural number) of
a letter and a word
(define (create-com-word letter a-word combinations)
  (cond
    [(zero? combinations) empty]
    [else
     (cons (insert-at-spot letter a-word combinations)
           (create-com-word letter a-word (sub1 combinations)))]))

;;insert-at-spot : symbol word number -> word
;;inserts a given symbol at a given spot in a word
(define (insert-at-spot letter a-word number)
  (cond
    [(and (empty? a-word) (= 1 number)) (cons letter empty)]
    [(empty? a-word) empty]
    [else
     (cond
       [(= 1 number) (cons letter (cons (first a-word) (insert-at-spot
letter (rest a-word) (sub1 number))))]
       [else (cons (first  a-word) (insert-at-spot letter (rest a-word)
(sub1 number)))])]))


;;how-many-combinations : word -> number
;;computes the amount of possible combinations of a specific word given a
word
(define (how-many-combinations a-word)
  (+  2 (- (how-many-letters a-word) 1)))


;;how-many-letters : a-word -> number
;;determines how many letters in a word
(define (how-many-letters a-word)
  (cond
    [(empty? a-word) 0]
    [else (+ 1 (how-many-letters (rest a-word)))]))

;;Tests
(equal? (arrangements (cons 'r(cons 'e empty)))
        (cons (cons 'e (cons 'r empty)) (cons (cons 'r (cons 'e empty))
empty)))

(equal? (arrangements (cons 'r(cons 'e(cons 'd  empty))))
        (cons (cons 'd (cons 'e (cons 'r empty)))
              (cons (cons 'd (cons 'r (cons 'e empty)))
                    (cons (cons 'r (cons 'd (cons 'e empty)))
                          (cons (cons 'e (cons 'd (cons 'r empty)))
                                (cons (cons 'e (cons 'r (cons 'd empty)))
                                      (cons (cons 'r (cons 'e (cons 'd
empty))) empty)))))))

End of program.

Thanks guys, any criticism will be appreciated!
_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Reply via email to