[racket] arrangements exercise

From: Sean Kemplay (sean.kemplay at gmail.com)
Date: Sun Jul 1 16:17:18 EDT 2012


I am working through HTDP and have re-visited the arrangements
exercise from chapter 12.

Looking back, I am wondering if my solution is a bit of a cheat - specifically :

(define (insert-everwhere/in-one-word s word)
  (make-words s word word))

Here is the entire solution - is there a better way I should be doing this?

(define (insert-symbol new old word)
    [(empty? word) empty]
    [(symbol=? (first word) old) (cons new (cons old (insert-symbol
new old (rest word))))]
    [else (cons (first word) (insert-symbol new old (rest word)))]))

(define (make-words s word1 word2)
    [(empty? word1) (cons (append word2 (cons s empty)) empty)]
    [else (cons (insert-symbol s (first word1) word2) (make-words s
(cdr word1) word2))]))

(define (insert-everwhere/in-one-word s word)
  (make-words s word word))

(define (insert-everywhere/in-all-words s low)
    [(empty? low) empty]
    [else (append (insert-everwhere/in-one-word s (first low))
(insert-everywhere/in-all-words s (rest low)))]))

(define (arrangements a-word)
    [(empty? a-word) (cons empty empty)]
    [else (insert-everywhere/in-all-words (first a-word)
            (arrangements (rest a-word)))]))

Any feedback welcome,

Sean Kemplay

Posted on the users mailing list.