[plt-scheme] HtDP newbie question, 12.4.2
Here's the answers to your questions:
Q: What do you mean with (rest ..) ? This doesn't look like my table.
A: Your table says, "(insert-everywhere/in-all-words s (rest low)," which means I would rewrite the whole function in the column? I guess I don't understand the purpose of the fourth column. The table is very helpful though.
[This isn't quite right! 'AT is NOT the representation of the English word "at".]
A: I was trying to use shorthand from Intermezzo 2:
'(a b c) This short-hand is an abbreviation for (list 'a 'b 'c)
Q: Why not take one step from row 1, which uses a list with one word. What would be the next case?
A: Um, two words. I have no idea why I did that.
Q: The second leap went wrong. Why? Is it possible that you JUMPED? Why don't you turn the above examples into examples for this function. And *please* do provide a contract, a purpose statement.
A: OK, this is where my big problem is. I'm not sure what you mean by JUMPED. Can you be more specific? I realize this is some kind of clue but I am too slow to get it.
Here's a full design recipe:
; Purpose: to insert a symbol between every letter of a single word
; Contract: symbol word --> list of words
; Example: (define (insert-in-single-word X AT). Result should be (XAT AXT ATX)
; Template:
; (define (insert-in-single-word s word)
; (cond
; [(empty? (rest word)) empty]
; [else (append (list (first word) s (rest word)) (insert-in-single-word s (rest word)))]))
Thanks very much.
Matthias Felleisen <matthias at ccs.neu.edu> wrote:
On Mar 27, 2008, at 1:20 PM, Cooke Kelsey wrote:
Dear Dr. Felleisen,
thanks so much for responding! Your suggestions really helped me to think about the problem.
Here's a table of examples, as you suggested:
;;Examples
; s | low |(first..)|(rest..) | result
Q: What do you mean with (rest ..) ? This doesn't look like my table.
;--------------------------------------------------------------------------------------------------------
; X | '(AT) |'(AT) |empty | '((XAT) (AXT) (ATX))
[This isn't quite right! 'AT is NOT the representation of the English word "at".]
; X | '((AB) (CD) (EF))|'(AB) |'((CD) (EF))| '((XAB) (AXB) (ABX) (XCD) (CXD) (CDX) (XEF) (EXF) (EFX))
This second row looks hyper-complicated.
Q: Why not take one step from row 1, which uses a list with one word. What would be the next case?
Here's an attempt to "go from the first columns to the last":
; Definitions:
(define (insert-everywhere/in-all-words s low)
(cond
[(empty? (rest low)) empty]
[else (append (insert-in-single-word s (first low)) (insert-everywhere/in-all-words s (rest low)))]))
Good the first leap went fine.
(define (insert-in-single-word s word)
(cond
[(empty? (rest word)) empty]
[else (append (list (first word) s (rest word)) (insert-in-single-word s (rest word)))]))
The second leap went wrong. Why?
Is it possible that you JUMPED? Why don't you turn the above examples into examples for this function. And *please* do provide a contract, a purpose statement.
-- Matthias
Unfortunately, the list of words gets shorter and shorter (sATX, sTX, sX). I tried to think of a way to add a recursive "prefix" that gets longer, but each time the function cycles the first letters seem to disappear into the void...
Matthias Felleisen <matthias at ccs.neu.edu> wrote:
On Mar 25, 2008, at 1:48 PM, Cooke Kelsey wrote: I am trying to learn HtDP by myself, and like Dave Yrueta I am stumped by 12.4.2.
[Oops I forgot to reply to his last message.]
; Purpose: to insert a symbol between every letter of every word, e.g. X, AT-->XAT, AXT, ATX.
Please reformulate the example in terms of symbols and lists.
;Function Template:
(define (insert-everywhere/in-all-words s low)
(cond
[(empty? (rest low))...]
[else ...(first low)...insert-everywhere/in-all-words s (rest low)]))
Note:
It is suggested to use "append", which is easy one or two times---(append x word) or (append (append (first word) x) (rest word))----but I can't see how to use this recursively for an arbitrary number of letters and words.
So far so good. Let's try what HtDP/2e will introduce:
Please make a table of the following shape:
s | low | (first low) | (insert-everywhere/in-all-words s (rest low) | expected result for (insert-... s low)
--------------------------------------------------------------------------------------------------------
X | AT | ? | use the purpose statement to determine this | (list `XAT' `AXT' `ATX')
As I said, you need to reformulate this in terms of symbols and lists. If it doesn't jump out at you how to go from the four columns to the last one, then make a couple of more examples. I am pretty sure it will. Here is the rule of thumb:
--> you know of a built-in function/primitive that does the work
--> you don't. in this case, you make a wish: a purpose statement and a contract for a helper function that does it for you.
You may also have to combine a primitives with functions.
Please report back -- Matthias
The fact that the "hint" refers to the keyword list, which the book hasn't covered yet, makes me wonder if it's even possible to answer this problem, given what we know so far...
Thanks very much,
Cooke
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search. _________________________________________________
For list-related administrative tasks:
http://list.cs.brown.edu/mailman/listinfo/plt-scheme
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
---------------------------------
Never miss a thing. Make Yahoo your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080327/7a537c65/attachment.html>