I think I got it! It produces the correct results, but I'm not sure if
it follows the design pattern properly:

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

;; Examples:
;; (cons 'a empty)
;; (cons 'h (cons 'i empty))
;; (cons 'c (cons 'a (cons 't empty)))
;; (cons 'l (cons 'a (cons 'p empty)))
;; (cons 'd (cons 'e (cons 'a (cons 'r empty))))

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

;; Examples:
;; (cons (cons 'h (cons 'i empty) empty)
;; (cons (cons 'h (cons 'i (cons 't empty)) (cons (cons 'a (cons 't
empty)) empty))
;; (cons (cons 'h (cons 'i empty)) (cons (cons 'a (cons 't empty))
(cons (cons 'd (cons 'e (cons 'a (cons 'r empty)))) empty)))

;; arrangements : 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/in-all-words (first a-word)
            (arrangements (rest a-word)))]))

;; Contract:
;; insert-everywhere/in-single-word : symbol word -> list-of-words

;; Purpose:
;; to insert a symbol everywhere in a single word

;; Examples:
;; (insert-everywhere/in-single-word 'a empty) should produce (list (list 'a))
;; (insert-everywhere/in-single-word 'b (list 'a)) should produce
(list (list 'b 'a) (list 'a 'b))
;; (insert-everywhere/in-single-word 'c (list 'a 'b)) should produce
(list (list 'c 'a 'b) (list 'a 'c 'b) (list 'a 'b 'c))

;; Template:
;; (define (insert-everywhere/in-single-word s w)
;;   (cond
;;     [(empty? w) ...]
;;     [else ... (first w) ... (insert-everywhere/in-single-word s w) ...]))
(define (insert-everywhere/in-single-word s w)
  (cond
    [(empty? w) (cons (cons s empty) empty)]
    [else (cons (cons s w) (insert-beginning/in-all-words (first w)
(insert-everywhere/in-single-word s (rest w))))]))

;; insert-beginning/in-all-words : s low -> low
;; to insert s at the beginning of all words in low
;; Examples:
;; (insert-beginning/in-all-words 'a empty) should produce empty
;; (insert-beginning/in-all-words 'i (cons 't empty)) should produce
(cons (cons 'i (cons 't empty)) empty)
;; (insert-beginning/in-all-words 'r (cons (cons 'e (cons 'd empty))
(cons (cons 'd (cons 'e empty)) empty))) should produce (cons (cons 'r
(cons 'e (cons 'd empty))) (cons (cons 'r (cons 'd (cons 'e empty)))
empty))
(define (insert-beginning/in-all-words s low)
  (cond
    [(empty? low) empty]
    [else (cons (cons s (first low)) (insert-beginning/in-all-words s
(rest low)))]))

;; insert-everywhere/in-all-words : symbol list-of-words -> list-of-words
;; to insert a symbol everywhere in a list of words
(define (insert-everywhere/in-all-words s low)
  (cond
    [(empty? low) empty]
    [else (append (insert-everywhere/in-single-word s (first low))
(insert-everywhere/in-all-words s (rest low)))]))


;; Tests:
(define a (list 'a))
(define it (list 'i 't))
(define red (list 'r 'e 'd))
(define read (list 'r 'e 'a 'd))

(insert-everywhere/in-single-word 'x empty)
(insert-everywhere/in-single-word 't a)
(insert-everywhere/in-single-word 'h it)
(insert-everywhere/in-single-word 'a red)
(check-expect (insert-everywhere/in-single-word 'a empty) (list (list 'a)))
(check-expect (insert-everywhere/in-single-word 't a) (list (list 't
'a) (list 'a 't)))
(check-expect (insert-everywhere/in-single-word 'h it) (list (list 'h
'i 't) (list 'i 'h 't) (list 'i 't 'h)))

Thank you everyone for all your help!
_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Reply via email to