-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw07.scm
36 lines (31 loc) · 963 Bytes
/
hw07.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
(define (cddr s) (cdr (cdr s)))
(define (cadr s)
(car (cdr s))
)
(define (caddr s)
(car (cdr (cdr s)))
)
(define (ordered? s)
(cond ( (null? s) #t) ; base case 1. empty list
( (null? (cdr s)) #t) ; base case 2. a list with size 1
( else (and (<= (car s) (cadr s))
(ordered? (cdr s)))))
)
(define (square x) (* x x))
(define (pow base exp)
(cond ( (= exp 1) base ) ; base^1 = base
( (= exp 0) 1) ; base^0 = 1
( (= 0 (modulo exp 2))
(begin
(define tmp (pow base (quotient exp 2))) ; store the value temporarily
(* tmp tmp)
)
)
( (= 1 (modulo exp 2))
(begin
(define tmp (pow base (quotient exp 2))) ; store the value temporarily
(* tmp tmp base)
)
)
)
)