-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_43.scm
37 lines (30 loc) · 951 Bytes
/
1_43.scm
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
#lang sicp
(define (square x) (* x x))
(define (inc x) (+ x 1))
(define (compose f g)
(lambda (x) (f (g x))))
(define (repeated f n)
(define (repeated-helper f composed n)
(if (= n 1)
composed
(repeated-helper f (compose f composed) (- n 1))))
(repeated-helper f f n))
((repeated square 2) 5)
((repeated inc 8) 3)
; since above is linear iterative
(define (double f-single)
(lambda (x) (f-single (f-single x))))
; this above is taken from one of the previous exercises
(define (repeated-log f n)
(define (even? x) (= (remainder x 2) 0))
(define (repeated-log-help f composed n)
(cond ((= n 0)
composed)
((even? n)
(repeated-log-help (double f) composed (/ n 2)))
(else
(repeated-log-help f (compose f composed) (- n 1)))))
(repeated-log-help f (lambda (x) x) n))
((repeated-log square 2) 5)
((repeated-log inc 8) 3)
((repeated-log inc 169) 11)