-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1.43
45 lines (39 loc) · 787 Bytes
/
ex1.43
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
(define (compose f g)
(lambda (x) (f (g x))))
(define (repeated1 f n)
(define (sub-fn f g counter)
(if (= counter 1)
f
(sub-fn (compose f g) g (- counter 1))))
(sub-fn f f n))
;; 1 ]=> ((repeated1 square 2) 5)
;;
;; ;Value: 625
;;
;; 1 ]=> ((repeated1 square 3) 5)
;;
;; ;Value: 390625
(define (repeated2 f n)
(if (= n 1)
f
(compose f (repeated2 f (- n 1)))))
;; 1 ]=> ((repeated2 square 2) 5)
;;
;; ;Value: 625
;;
;; 1 ]=> ((repeated2 square 3) 5)
;;
;; ;Value: 390625
(define (repeated3 f n)
(define (iter counter result)
(if (= counter 1)
result
(iter (- counter 1) (compose f result))))
(iter n f))
;; 1 ]=> ((repeated3 square 2) 5)
;;
;; ;Value: 625
;;
;; 1 ]=> ((repeated3 square 3) 5)
;;
;; ;Value: 390625