-
Notifications
You must be signed in to change notification settings - Fork 0
/
e.1.43.rkt
55 lines (44 loc) · 1 KB
/
e.1.43.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang racket
(require (submod "e.1.42.rkt" export))
(define (square x) (* x x))
;;; recursive
(define (repeated0 f n)
(lambda (x)
(define (rec i)
(if (zero? i)
x
(f (rec (- i 1)))))
(rec n)))
;;; recursive
(define (repeated1 f n)
(lambda (x)
(if (zero? n)
x
((compose f (repeated1 f (- n 1)))
x))))
;;; iterative-recursive
(define (repeated2 f n)
(lambda (x)
(define (iter i co)
'(display (format "~a ~a\n" i x))
(if (zero? i)
(co x)
(iter (- i 1)
(lambda (x)
(co (f x))))))
(iter n (lambda (x) x))))
;;; iterative-recursive
(define (repeated3 f n)
(define (iter i co)
'(display (format "~a ~a\n" i x))
(if (zero? i)
co
(iter (- i 1)
(lambda (x)
(co (f x))))))
(iter n (lambda (x) x)))
(module+ test
((repeated0 square 5) 10)
((repeated1 square 5) 10)
((repeated2 square 5) 10)
((repeated3 square 5) 10))