Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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 12687dd9 2023-08-04 jrmu #|
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu Exercise 21.1.1. Define tabulate, which is the abstraction of the following two functions:
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu ;; tabulate-sin : number -> lon
9 12687dd9 2023-08-04 jrmu ;; to tabulate sin between n
10 12687dd9 2023-08-04 jrmu ;; and 0 (inclusive) in a list
11 12687dd9 2023-08-04 jrmu (define (tabulate-sin n)
12 12687dd9 2023-08-04 jrmu (cond
13 12687dd9 2023-08-04 jrmu [(= n 0) (list (sin 0))]
14 12687dd9 2023-08-04 jrmu [else
15 12687dd9 2023-08-04 jrmu (cons (sin n)
16 12687dd9 2023-08-04 jrmu (tabulate-sin (sub1 n)))]))
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu ;; tabulate-sqrt : number -> lon
19 12687dd9 2023-08-04 jrmu ;; to tabulate sqrt between n
20 12687dd9 2023-08-04 jrmu ;; and 0 (inclusive) in a list
21 12687dd9 2023-08-04 jrmu (define (tabulate-sqrt n)
22 12687dd9 2023-08-04 jrmu (cond
23 12687dd9 2023-08-04 jrmu [(= n 0) (list (sqrt 0))]
24 12687dd9 2023-08-04 jrmu [else
25 12687dd9 2023-08-04 jrmu (cons (sqrt n)
26 12687dd9 2023-08-04 jrmu (tabulate-sqrt (sub1 n)))]))
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu 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
29 12687dd9 2023-08-04 jrmu
30 12687dd9 2023-08-04 jrmu |#
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;tabulate : (number -> X) number -> (listof X)
33 12687dd9 2023-08-04 jrmu ;Given op and n, return the value of (op i) where i is an integer, for integers from n to 0, inclusive.
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu (define (tabulate op n)
36 12687dd9 2023-08-04 jrmu (cond
37 12687dd9 2023-08-04 jrmu [(= n 0) (list (op 0))]
38 12687dd9 2023-08-04 jrmu [else (cons (op n)
39 12687dd9 2023-08-04 jrmu (tabulate op (sub1 n)))]))
40 12687dd9 2023-08-04 jrmu
41 12687dd9 2023-08-04 jrmu ;tabulate-sin : number -> (listof number)
42 12687dd9 2023-08-04 jrmu ;Tabulate the sin of all integers from n to 0 inclusive.
43 12687dd9 2023-08-04 jrmu (define (tabulate-sin n)
44 12687dd9 2023-08-04 jrmu (tabulate sin n))
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu ;tabulate-sqrt : number -> (listof number)
47 12687dd9 2023-08-04 jrmu ;Tabulate the sqrt of all integers from n to 0 inclusive.
48 12687dd9 2023-08-04 jrmu (define (tabulate-sqrt n)
49 12687dd9 2023-08-04 jrmu (tabulate sqrt n))