[racket] Tabulate from build-list

From: Sean Kemplay (sean.kemplay at gmail.com)
Date: Tue Jul 31 08:12:32 EDT 2012

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
>

Posted on the users mailing list.