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