[plt-scheme] HTDP: 9.5.5

From: Todd O'Bryan (toddobryan at gmail.com)
Date: Sat Sep 13 22:34:03 EDT 2008

That's the absolutely essential part! Let's look at the template for a
list-of-number function:

;; lon-fun: list-of-number -> ?
;; ?
(define (lon-fun alon)
  (cond
    [(empty? alon) ]
    [(cons? alon) (first alon)
                  (lon-fun (rest alon)]))

Now replace lon-fun with convert:

;; convert: list-of-number -> number
;; consumes a list of digits from least to most significant
;; produces the corresponding number
(define (convert alon)
  (cond
    [(empty? alon) ]
    [(cons? alon) (first alon)
                  (convert (rest alon)]))

(check-expect (convert (cons 4 (cons 3 (cons 2 (cons 1 empty))))) 1234)

The template itself gives you two pieces when the list is non-empty,
the first element and the function called on the rest of the list. In
this example, you get 4 and 123. Your job is to figure out how to
combine those two pieces together to get the answer you desire.

That's the key point that I constantly push with my students. The
shape of the data encourages a particular shape for the functions that
manipulate the data. The shape of the function already does much of
the work for you. Your job is to look at what you get "for free" and
figure out how to massage it to get what you want.

HTH,
Todd
On Sat, Sep 13, 2008 at 7:43 PM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> Now discover Jens's hint by following the design recipe.
>
>
> On Sep 13, 2008, at 7:33 PM, Grant Rettke wrote:
>
>> On Sat, Sep 13, 2008 at 5:57 PM, Jens Axel Soegaard
>> <jensaxel at soegaard.net> wrote:
>>>
>>> Think recursively:
>>>
>>>  1234 = 4 + 10*123
>>
>> I see! Thanks Jens.
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.