<div class="gmail_quote">---------- Forwarded message ----------<br>From: "Sean Kemplay" <<a href="mailto:sean.kemplay@gmail.com">sean.kemplay@gmail.com</a>><br>Date: Jul 31, 2012 2:32 PM<br>Subject: Re: [racket] Tabulate from build-list<br>
To: "Matthias Felleisen" <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>><br><br type="attribution">Thanks Matthias,<br>
<br>
(define (tabulate f n)<br>
(build-list (+ n 1) (lambda (x) (f (- n x)))))<br>
<br>
I am glad I went the other way as well - I learnt something<br>
implementing it - and a lot from the transformation to the one liner.<br>
<br>
Kind regards,<br>
Sean<br>
<br>
On Tue, Jul 31, 2012 at 1:43 PM, Matthias Felleisen<br>
<div class="elided-text"><<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>> wrote:<br>
><br>
> Some suggestions:<br>
><br>
> 1. (+ (* -1 x) y) is (- y x)<br>
> 2. f is in scope of the entire definition; there is no need to pass it along.<br>
> 3. Once you fix 2, you see that g == (inverter n) == (lambda (x) (f (- n x))<br>
> 4. So why not replace all occurrences of g with the lambda expression?<br>
><br>
> Voilą, you have a one-line definition. Send it back when done.<br>
><br>
><br>
> On Jul 31, 2012, at 8:12 AM, Sean Kemplay wrote:<br>
><br>
>> Ok, not sure if I went down the same route as Matthias was alluding to<br>
>> but here is my solution -<br>
>><br>
>> (define (tabulate f n)<br>
>> (local<br>
>> ((define (inverter y f) (lambda (x) (f (+ (* x -1) y))))<br>
>> (define g (inverter n f)))<br>
>> (build-list (+ n 1) g)))<br>
>><br>
>><br>
>> (check-expect (tabulate (lambda (x) x) 3) '(3 2 1 0))<br>
>><br>
>> I used a curried function to 'invert' each number in place before the<br>
>> function provided to build list is applied.<br>
>><br>
>> Regards,<br>
>> Sean<br>
>><br>
>> On Mon, Jul 30, 2012 at 10:32 PM, Matthias Felleisen<br>
>> <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>> wrote:<br>
>>><br>
>>> Here is how I suggest our freshmen to find this function:<br>
>>><br>
>>> ;; tabulate : (x -> y) Nat -> (listof y) ;; <------- MF: not fix of signature<br>
>>> ;; to tabulate f between n<br>
>>> ;; and 0 (inclusive) in a list<br>
>>><br>
>>> (check-expect (tabulate (lambda (x) x) 3) '(3 2 1 0))<br>
>>><br>
>>> (define (tabulate.v0 f n)<br>
>>> (cond<br>
>>> [(= n 0) (list (f 0))]<br>
>>> [else<br>
>>> (cons (f n)<br>
>>> (tabulate f (sub1 n)))]))<br>
>>><br>
>>> ;; Using build-list<br>
>>> ;; build-list : N (N -> X) -> (listof X)<br>
>>> ;; to construct (list (f 0) ... (f (- n 1)))<br>
>>><br>
>>> (define (tabulate f n)<br>
>>> (local (;; Nat -> Y ;; <-------------- MF: you know you want build-list, design (!) the 'loop' function now<br>
>>> (define (g i) ... f n i ...)) ;; <---- this is the data that's available<br>
>>> (build-list (+ n 1) g)))<br>
>>><br>
>>> Also see '2e'.<br>
>>><br>
>>><br>
>>> On Jul 30, 2012, at 4:10 PM, Sean Kemplay wrote:<br>
>>><br>
>>>> Hello,<br>
>>>><br>
>>>> I am looking at the exercise from htdp 1e on building the tabulate<br>
>>>> function from build-list.<br>
>>>><br>
>>>> Would I be on the right track that I need to create a function to<br>
>>>> reverse the result of (build-list (+ n 1) f) to do this?<br>
>>>><br>
>>>> the function this is to emulate -<br>
>>>><br>
>>>> ;; tabulate : (x -> y) x -> (listof y)<br>
>>>> ;; to tabulate f between n<br>
>>>> ;; and 0 (inclusive) in a list<br>
>>>> (define (tabulate f n)<br>
>>>> (cond<br>
>>>> [(= n 0) (list (f 0))]<br>
>>>> [else<br>
>>>> (cons (f n)<br>
>>>> (tabulate f (sub1 n)))]))<br>
>>>><br>
>>>> Using build-list<br>
>>>> ;; build-list : N (N -> X) -> (listof X)<br>
>>>> ;; to construct (list (f 0) ... (f (- n 1)))<br>
>>>> (define (build-list n f) ...)<br>
>>>><br>
>>>> Regards,<br>
>>>> Sean<br>
>>>> ____________________<br>
>>>> Racket Users list:<br>
>>>> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
>>><br>
><br>
</div></div>