[racket] arrangements exercise
Hi Sean --
Don't know if it is a "cheat," but I can tell you from experience your solution diverges from the preferred one at the "insert-everywhere/in-one-word" function.
Try working through this exercise by systematically applying the design recipe. That is, take the time to make your data definitions explicit, and begin the definition of each helper function with a contract, purpose statement, and template. In my opinion, more than any other exercise in HtDP, the solution to this exercise relies on paying very close attention to how the data definitions, function contracts and function templates interact. If you allow the data definitions to shape the function templates, and are rigorous about ensuring the function designs conform to their contracts, the solution should fall into place.
Good luck!
Dave Yrueta
On Jul 1, 2012, at 1:17 PM, Sean Kemplay wrote:
> 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
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users