[plt-scheme] HtDP 12.4.2 question details

From: Grant Rettke (grettke at acm.org)
Date: Fri Apr 3 07:52:39 EDT 2009

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))

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.

Posted on the users mailing list.