[racket] Some guidance and help for Section 4.5.1
On Apr 27, 2013, at 10:35 PM, Da Gamer wrote:
> First, I'm working on problem: How To Design Programs, 2nd Edition (Chapter 4, Section 4.5.1).
> [...]
> However, the code feels .... wrong. As if I didn't do it the Scheme way. It's like writing C with classes and not idiomatic C++. It works, but it's wrong.
>
> So any tips, hints, pointing in the right direction, would be greatly appreciated.
[In the following, I'm using text from your first solution, but what I'm saying applies equally well to your second solution.]
It looks like the problem you're up against is that, beyond insert-everywhere/in-one-word, the function specifications are vague:
; 1-str list-of-word -> list-of-word
; create combinations of word in list-of-word
(define (create-combinations ins-ltr a-low) ...)
One of the questions we ask in the Design Recipes is, "What is the result of the recursive call producing?" Here, for example, the recursive call is
(create-combinations ins-ltr (rest a-low))
and according to the purpose statement, it could be said to produce "combinations of word in (rest a-low)". But what does that mean? The purpose statement speaks as though word is a parameter, but it's not -- you have ins-ltr and a-low. Also, what does it mean by "combinations"? This is not clear, and so you can clarify both by rewriting the purpose statement and choosing a more descriptive function name.
So, here is what I would recommend: Ask yourself what the function must produce from (first a-word) and (insert-everywhere/in-one-word a-1str (rest a-word)) in insert-everywhere/in-one-word. And don't be satisifed until you have an answer that contains no vagueness at all.
Now, answering that question is probably not easy, and this is where the rest of the Design Recipe is useful:
1) Write a couple of test cases for insert-everywhere/in-one-word. Specific data is almost always easier to think about than abstract names. At very least, make a test case for a 1-letter word w1 and another for a 2-letter word w2, such that w1 is (rest w2).
2) In those test cases, identify what (first a-word) and (insert-everywhere/in-one-word a-1str (rest a-word)) are. Compare those data to your test cases' desired results, since those results are what create-combinations must produce from those two pieces of data. Use these data to make a test case for create combinations.
3) If you can come up with a plain-English description of what create-combinations must do, then great! write a purpose statement for it (and perhaps change the name to something nicely descriptive of what you want). If not, try making a larger test case (e.g., a word w such that (rest w) is your two-letter word from earlier), and repeat the process. Eventually you will most likely see a pattern you can describe.
This is a tough problem, but taking the time to find the simple solution is worth it IMO. Your intuition is good. Keep at it.
Best,
jmj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130427/817130fb/attachment.html>