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.2. Define fold, which is the abstraction of the following two functions:
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu ;; sum : (listof number) -> number
9 12687dd9 2023-08-04 jrmu ;; to compute the sum of
10 12687dd9 2023-08-04 jrmu ;; the numbers on alon
11 12687dd9 2023-08-04 jrmu (define (sum alon)
12 12687dd9 2023-08-04 jrmu (cond
13 12687dd9 2023-08-04 jrmu [(empty? alon) 0]
14 12687dd9 2023-08-04 jrmu [else (+ (first alon)
15 12687dd9 2023-08-04 jrmu (sum (rest alon)))]))
16 12687dd9 2023-08-04 jrmu
17 12687dd9 2023-08-04 jrmu ;; product : (listof number) -> number
18 12687dd9 2023-08-04 jrmu ;; to compute the product of
19 12687dd9 2023-08-04 jrmu ;; the numbers on alon
20 12687dd9 2023-08-04 jrmu (define (product alon)
21 12687dd9 2023-08-04 jrmu (cond
22 12687dd9 2023-08-04 jrmu [(empty? alon) 1]
23 12687dd9 2023-08-04 jrmu [else (* (first alon)
24 12687dd9 2023-08-04 jrmu (product (rest alon)))]))
25 12687dd9 2023-08-04 jrmu Don't forget to test fold.
26 12687dd9 2023-08-04 jrmu After fold is defined and tested, use it to define append, which juxtaposes the items of two lists or, equivalently, replaces empty at the end of the first list with the second list:
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu (equal? (append (list 1 2 3) (list 4 5 6 7 8))
29 12687dd9 2023-08-04 jrmu (list 1 2 3 4 5 6 7 8))
30 12687dd9 2023-08-04 jrmu Finally, define map using fold.
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu Compare the four examples to formulate a contract. Solution
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu |#
35 12687dd9 2023-08-04 jrmu
36 12687dd9 2023-08-04 jrmu ;fold : (X Y -> Y) (listof X) Y -> Y
37 12687dd9 2023-08-04 jrmu ;Given op and alox, return the value of op applied cumulatively on every value in alox. The value base specifies a value that is the base from which fold operates upon.
38 12687dd9 2023-08-04 jrmu
39 12687dd9 2023-08-04 jrmu (define (fold op alox base)
40 12687dd9 2023-08-04 jrmu (cond
41 12687dd9 2023-08-04 jrmu [(empty? alox) base]
42 12687dd9 2023-08-04 jrmu [else (op (first alox) (fold op (rest alox) base))]))
43 12687dd9 2023-08-04 jrmu
44 12687dd9 2023-08-04 jrmu ;sum : (listof number) -> number
45 12687dd9 2023-08-04 jrmu ;Given alon, adds all the numbers in the list.
46 12687dd9 2023-08-04 jrmu (define (sum alon)
47 12687dd9 2023-08-04 jrmu (fold + alon 0))
48 12687dd9 2023-08-04 jrmu
49 12687dd9 2023-08-04 jrmu ;product : (listof number) -> number
50 12687dd9 2023-08-04 jrmu ;Given alon, multiplies all the numbers in the list.
51 12687dd9 2023-08-04 jrmu (define (product alon)
52 12687dd9 2023-08-04 jrmu (fold * alon 1))
53 12687dd9 2023-08-04 jrmu
54 12687dd9 2023-08-04 jrmu ;append : (listof X) (listof Y)
55 12687dd9 2023-08-04 jrmu ;Given list1 and list2, append list2 to the end of list1.
56 12687dd9 2023-08-04 jrmu (define (append! list1 list2)
57 12687dd9 2023-08-04 jrmu (fold cons list1 list2))
58 12687dd9 2023-08-04 jrmu
59 12687dd9 2023-08-04 jrmu (append! '(Hi my name is Joe) '(Hi my name is Aaron))
60 12687dd9 2023-08-04 jrmu