[racket] arrangements exercise
Ok, here is another shot - this time, like arrangements, making a list
of words from the base case of insert-everywhere/in-all-words to feed
to make words.
;; Word -> [Listof Word]
(check-expect (arrangements '(a)) (list '(a)))
(check-expect (arrangements '(a b)) (list '(a b) '(b a)))
(define (arrangements a-word)
(cond
[(empty? a-word) (cons empty empty)]
[else
(insert-everywhere/in-all-words (first a-word) (arrangements
(rest a-word)))]))
(define (insert-everywhere/in-all-words s a-low)
(cond
[(empty? a-low) empty]
[else (append (insert-everywhere/word s (first a-low))
(insert-everywhere/in-all-words s (rest a-low)))]))
;; insert-everywhere/word symbol word -> list of words
;;
;; given a symbol and a word, insert the symbol in every
;; posible position between each letter in the word
;; producing a list of words
(check-expect (insert-everywhere/word 'a '()) '((a)))
(check-expect (insert-everywhere/word 'a '(b)) '((a b) (b a)))
(check-expect (insert-everywhere/word 'a '(b c)) '((a b c) (b a c) (b c a)))
(define (insert-everywhere/word s word)
(cond
[(empty? word) (cons (cons s empty) empty)]
[else (cons (cons s word)
(make-words (first word)
(insert-everywhere/word s (rest word))))]))
;; make-words symbol list of words -> list of words
;; Given a symbol and a list of (partial) words, start
;; back filling with the symbol producing a new list of words
(check-expect (make-words 'b empty) empty)
(check-expect (make-words 'b '((a))) '((b a)))
(check-expect (make-words 'c '((b a) (a))) '((c b a) (c a)))
(define (make-words s low)
(cond
[(empty? low) empty]
[else (cons (cons s (first low)) (make-words s (rest low)))]))
On Sat, Jul 7, 2012 at 12:18 PM, Sean Kemplay <sean.kemplay at gmail.com> wrote:
> Thanks Matthias,
>
> I will go through 2e up until the arrangements exercise then make the
> examples to post to the list.
>
> Thank you for your time it is much appreciated.
>
> Sean
>
> On Fri, Jul 6, 2012 at 1:21 PM, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>>
>> On Jul 6, 2012, at 5:48 AM, Sean Kemplay wrote:
>>
>> I am going through htdp first edition (at chapter 17 which refers to
>> arrangements from chapter 12 which is why I revisited the exercise). Would
>> you suggest continuing with this edition or moving to the draft second
>> edition
>>
>>
>> I would switch to '2e' if I were you. It's more fun but you will learn
>> mostly the same ideas. ('2e' provides an event-driven program context as
>> playground for developing design skills. '1e' provides no real context.)
>>
>>
>> Do you mean the contract for insert-everywhere/word should be symbol word ->
>> list of words? And will the words returned be the same length? Ie am I
>> aiming for the same result as the current definition?
>>
>>
>> Letter List-of-Words -> List-of-Words
>> insert letter l in all positions in all words in lw
>>
>> (define (insert-everywhere/all-words l lw)
>> lw)
>>
>>
>> Letter Word -> List-of-Words
>> insert letter l in all positions in one word (w)
>>
>> (define (insert-everywhere/1word l w)
>> (list w))
>>
>> ;; ---
>>
>> I recommend you make up examples (with check-expect) first and send them
>> back to the list.
>> The above 'stub' implementations should fail most of your examples. (A
>> stub/silly/stupid/obviously-wrong
>> implementation is something '2e' suggests.)
>>
>>
>>
>>