[racket] Tabulate from build-list

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Jul 30 17:32:37 EDT 2012

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.