[plt-scheme] HtDP 12.4.2 question details

From: Todd O'Bryan (toddobryan at gmail.com)
Date: Fri Apr 3 21:20:13 EDT 2009

1. Yes, use list. Trying to do this problem without list is painful,
but the pain serves no useful learning purpose.

2. You're on the right track with this function:

;; insert-everywhere-in-one-word: symbol word -> list-of-word
;; consumes: a symbol and a word
;; produces: the list of words you get by inserting the symbol in
every position in the word

Take a look at the template for a word-function. Since a word is
really just a list-of-symbol, you get

(define (word-fun a-word)
  (cond
    [(empty? a-word) ...]
    [(cons? a-word) (first a-word)
                           (word-fun (rest a-word))]))

In this case, in other words, the template already gives you

(insert-everywhere-in-one-word sym (rest a-word))

If you take an example, say (insert-everywhere-in-one-word 'a (list 'x
'y 'z)), the recursive call will give you

(list (list 'a 'y 'z) (list 'y 'a 'z) (list 'y 'z 'a))

What you want for the final answer is

(list (list 'a 'x 'y 'z) (list 'x 'a 'y 'z) (list 'x 'y 'a 'z) (list
'x 'y 'z 'a))

If you can figure out how to get from what the template gives you to
what you want, you're done. (By the way, one of the really interesting
things about this problem is that longer examples make it easier to
see the pattern. You may be having trouble because you're trying
things like (list 'e 'r) which aren't long enough to make you see the
whole thing.)

Hope that helps,
Todd

On Fri, Apr 3, 2009 at 7:52 AM, Grant Rettke <grettke at acm.org> wrote:
> Hi,
> I’ve got a few questions about 12.4.2:
> In my first attempt, I went through the problem and came up with the
> wishlist that I would like a function which, given a symbol and a
> word, would create a list of words where the symbol was inserted at
> the beginning, end, and in between every character once.  I surely
> didn’t follow the recipe, though, because it didn't work:
>
> (arrangements (cons 'e (cons 'r empty)))
>
> does not produce
>
> (cons (cons 'e (cons 'r empty))
>       (cons (cons 'r (cons 'e empty))
>             empty))
>
> as the problem describes.
>
> The thing is that I ignored what I saw and defined (insert-everywhere
> char word) first:
>
> (define (insert-everywhere char word)
>  (insert-everywhere-h char word 0))
>
> ; insert-everywhere-h : symbol word integer -> list-of-words
> ; to generate a list-of-words by inserting char into
> ; word in every possible position
>
> This function is like the insert functions in this chapter, and stops
> when it reaches the length of the word (which is the last position).
>
> The examples defines that insert-everywhere/in-all-words consumes a
> symbol and a list of words, and the example
> reveals that the first time you call insert-everywhere/in-all-words,
> the list-of-words that you pass it is:
>
> (insert-everywhere/in-all-words 'r (cons empty empty))
>
> According to this:
>
> ; 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.
>
> That is valid, but why did you pass that rather than empty, which also
> would have been a list-of-words?
>
> Is it OK to use the list function in this question? Should we? The
> book mentions its use, but technically doesn't introduce it until
> Chapter 13.
>
> Does one of the wishlist functions insert a symbol into a word? I was
> happy with the function that did not, but not sure of the pattern
> where I have to pass in the "start index" for insertions, 0, and
> increment on each recursion, until it reaches the end of the word.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.