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