Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 21.1.2) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
4 #|
6 Exercise 21.1.1. Define tabulate, which is the abstraction of the following two functions:
8 ;; tabulate-sin : number -> lon
9 ;; to tabulate sin between n
10 ;; and 0 (inclusive) in a list
11 (define (tabulate-sin n)
12 (cond
13 [(= n 0) (list (sin 0))]
14 [else
15 (cons (sin n)
16 (tabulate-sin (sub1 n)))]))
18 ;; tabulate-sqrt : number -> lon
19 ;; to tabulate sqrt between n
20 ;; and 0 (inclusive) in a list
21 (define (tabulate-sqrt n)
22 (cond
23 [(= n 0) (list (sqrt 0))]
24 [else
25 (cons (sqrt n)
26 (tabulate-sqrt (sub1 n)))]))
28 Be sure to define the two functions in terms of tabulate. Also use tabulate to define a tabulation function for sqr and tan. What would be a good, general contract? Solution
30 |#
32 ;tabulate : (number -> X) number -> (listof X)
33 ;Given op and n, return the value of (op i) where i is an integer, for integers from n to 0, inclusive.
35 (define (tabulate op n)
36 (cond
37 [(= n 0) (list (op 0))]
38 [else (cons (op n)
39 (tabulate op (sub1 n)))]))
41 ;tabulate-sin : number -> (listof number)
42 ;Tabulate the sin of all integers from n to 0 inclusive.
43 (define (tabulate-sin n)
44 (tabulate sin n))
46 ;tabulate-sqrt : number -> (listof number)
47 ;Tabulate the sqrt of all integers from n to 0 inclusive.
48 (define (tabulate-sqrt n)
49 (tabulate sqrt n))