# [plt-scheme] HtDP newbie question, 12.4.2

 From: Cooke Kelsey (cookekelsey at yahoo.com) Date: Fri Mar 28 12:34:10 EDT 2008 Previous message: [plt-scheme] HtDP newbie question, 12.4.2 Next message: [plt-scheme] HtDP newbie question, 12.4.2 Messages sorted by: [date] [thread] [subject] [author]

```OK, here's a simple function that adds a letter to a word:

(append (list first-letter) word))

(add-first-letter 'A (list 'X 'T 'E))=(list 'A 'X 'T 'E')

So my question is, where does (list 'A 'T 'E) come from?  In your table below, you say "the recursion: "X" "TE" --> "XTE" "TXE" "TEX".  Actually I have never been able to create such a recursion.  That's the big mystery to me.

I just read Dave Yrueta's encouraging note, and he says I need to focus on your hint about adding 'A to the list, "in the simplest and most natural way."  Well, all I can think of is to append it, like I did above.  But there is no recursive element, and I cannot call it or plug it into the main recursive function.  It is floating out in space.

Matthias Felleisen <matthias at ccs.neu.edu> wrote:

Okay you have gotten to the point where you're jumping even faster. Slogan:

furiously fast programming leads to nowhere.

;; ---

All I was getting at is a simple point:

Your recursive calls in insert-everywhere/one-word produced almost all the work in the recursion. Indeed, it always produces all but one word, except that the first letter from the original word is missing. Example:

given:          the recursion                           in the final result I want                    plus
'X   "ATE"     "X" "TE" --> "XTE" "TXE" "TEX"     "AXTE" "ATXE" "ATXE" "ATEX"           "XATE"

Given the recursive result (the list of words that are too short by the first letter in the orginal word) can you design a function that adds this letter (no matter what it is) to the front of all these words you have gotten back?

DONE?

After that: what is missing?

-- Matthias

On Mar 28, 2008, at 11:51 AM, Cooke Kelsey wrote:
OK, that helps me a little.  If I insert "cons (first word)," then I can rebuild the word:

(define (cons-function word)
(cond
[(empty? (rest word)) word]
[else (cons (first word) (cons-function (rest word)))]))

(cons-function (list 'A 'T 'W)) = (list 'A 'T 'W)

The problem is how to add this to a shrinking function:

(define (shrinking-function s word)
(cond
[(empty? (rest word)) empty]
[else (list (append (list s) word) (shrinking-function s (rest word)))]))

(This shrinking function isn't exactly what I want, but at least it gives me (list 'X 'A 'T 'W) and (list 'X 'T 'W)).

So I append them together:

(append (cons-function (list 'A 'T 'W)) (shrinking-function 'X (list 'A 'T 'W)))

And I get this:

(list (list 'X 'A 'T 'W) (list (list 'X 'T 'W) empty)) (list 'A 'T 'W (list 'X 'A 'T 'W) (list (list 'X 'T 'W) empty))

Garbage!

As a last resort, I try to combine cons and shrinking functions together:

(define (cons+shrinking-function s word)
(cond
[(empty? (rest word)) word]
[else (append (cons (first word) (cons+shrinking-function 'X (rest word))) (list s) (rest word))]))

(cons+shrinking-function 'X (list 'A 'T 'W)) = (list 'A 'T 'W 'X 'W 'X 'T 'W)

It looks kind of nice, but it is even further from the expected result.

Matthias Felleisen <matthias at ccs.neu.edu> wrote:

How about (cons 'A (list 'X 'T)) = (list 'A 'X 'T)

On Mar 28, 2008, at 10:53 AM, Cooke Kelsey wrote:
(append (list 'A) (list 'X 'T)) = (list 'A 'X 'T)

Matthias Felleisen <matthias at ccs.neu.edu> wrote:

Here is a definition of 'adding in the missing element':

* If a list X misses the symbol 'A (at the front), how do you get a list that has the 'A and all of X?

* X = (list 'X 'T) and 'A --> I want (list 'A 'X 'T). How do I do that?

-- Matthias

---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.

---------------------------------
Never miss a thing. Make Yahoo your homepage.

---------------------------------
Never miss a thing.   Make Yahoo your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...