[racket] Some guidance and help for Section 4.5.1
On Apr 28, 2013, at 2:46 AM, Jordan Johnson <jmj at fellowhuman.com> wrote:
> 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.
This is what I call an "inventory with values". Before you get to this stage, you've learned to write down (in comments) an "inventory" of expressions you're likely to need inside the function. Start with the parameters, and any global variables that are obviously related. If any of those are of struct types, you might want to add expressions getting at their fields. If any of those are of variant types (like "list", which is "either an empty list or a non-empty list"), write a conditional that distinguishes the cases, and then within each case, you can list the likely expressions. So for a typical function-on-lists, you get
(define (my-function numbers)
; numbers a list of numbers
(cond [(empty? numbers)
…
]
[(cons? numbers)
; (first numbers) a number
; (rest numbers) a list of numbers
; (my-function (rest numbers)) whatever type this returns
…
]))
Then comes the "inventory with values" part: pick a not-too-simple example (say, a list of three or four elements) from the test cases that you've already written (if you haven't already written several test cases, with right answers, stop and do that!), and next to each of the comment expressions, write down its value for that example. Also add another commented line saying "right answer", and write down ITS value for that example. If there's another conditional, other than the "is it empty or not?" question above, choose at least two not-too-simple examples, one for each case. The result might look like
(define (my-function numbers)
; numbers a list of numbers '(3 8 7 2)
(cond [(empty? numbers)
…
]
[(cons? numbers)
; (first numbers) a number 3
; (rest numbers) a list of numbers '(8 7 2)
; (my-function (rest numbers)) whatever type this returns 278
; right answer number 2783
…
]))
Stephen Bloch
sbloch at adelphi.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130428/fef0c951/attachment.html>