[racket] arrangements exercise
Hello,
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)
(cond
[(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)
(cond
[(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)
(cond
[(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)
(cond
[(empty? a-word) (cons empty empty)]
[else (insert-everywhere/in-all-words (first a-word)
(arrangements (rest a-word)))]))
Any feedback welcome,
Thanks,
Sean Kemplay