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-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 ;fold : (X Y -> Y) (listof X) Y -> Y
5 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.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu (define (fold op alox base)
8 12687dd9 2023-08-04 jrmu (cond
9 12687dd9 2023-08-04 jrmu [(empty? alox) base]
10 12687dd9 2023-08-04 jrmu [else (op (first alox) (fold op (rest alox) base))]))
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;op : (X Y -> Y)
13 12687dd9 2023-08-04 jrmu ;alox : (listof X)
14 12687dd9 2023-08-04 jrmu ;base : Y
15 12687dd9 2023-08-04 jrmu ;(first alox) : X
16 12687dd9 2023-08-04 jrmu ;(rest alox) : (listof X)
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu ;map1 : (listof X) -> (listof Y)
19 12687dd9 2023-08-04 jrmu ;Given alox, map1 each element of type X from alox to an element in type Y according to the function f to return a (listof Y).
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu (define (map1 alox)
22 12687dd9 2023-08-04 jrmu (fold consf alox empty))
23 12687dd9 2023-08-04 jrmu
24 12687dd9 2023-08-04 jrmu ;Test
25 12687dd9 2023-08-04 jrmu ;(map1 consf alox)
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu ;(consf (first alox) (fold consf (rest alox) empty))
28 12687dd9 2023-08-04 jrmu ;(cons (f (first alox)) (fold f (rest alox) empty))
29 12687dd9 2023-08-04 jrmu ;(f X)
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;consf : X Y -> Y
32 12687dd9 2023-08-04 jrmu ;Given x, y, map1 x to an element Y and then append to the beginning of y.
33 12687dd9 2023-08-04 jrmu (define (consf x y)
34 12687dd9 2023-08-04 jrmu (cons (f x) y))
35 12687dd9 2023-08-04 jrmu
36 12687dd9 2023-08-04 jrmu ;f : number -> number
37 12687dd9 2023-08-04 jrmu ;Add 5.
38 12687dd9 2023-08-04 jrmu (define (f x)
39 12687dd9 2023-08-04 jrmu (+ x 5))
40 12687dd9 2023-08-04 jrmu
41 12687dd9 2023-08-04 jrmu #|
42 12687dd9 2023-08-04 jrmu (define (map1 f lon)
43 12687dd9 2023-08-04 jrmu (cond
44 12687dd9 2023-08-04 jrmu [(empty? lon) empty]
45 12687dd9 2023-08-04 jrmu [else (cons (f (first lon))
46 12687dd9 2023-08-04 jrmu (map1 f (rest lon)))]))
47 12687dd9 2023-08-04 jrmu
48 12687dd9 2023-08-04 jrmu |#
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu (equal? (map1 '(1 3 2 5 1 4 0 2 6 8 6 9 2 3 4 5 1 8 2))
51 12687dd9 2023-08-04 jrmu (list 6 8 7 10 6 9 5 7 11 13 11 14 7 8 9 10 6 13 7))