-
Notifications
You must be signed in to change notification settings - Fork 3
/
envs.scm
48 lines (42 loc) · 1.2 KB
/
envs.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
38
39
40
41
42
43
44
45
46
47
48
(define (dist x1 y1 x2 y2)
(define dx (- x2 x1))
(define dy (- y2 y1))
; (define dz dx)
(define (sq x) (* x x (if (= x 1) 1 (sq 1))))
(sqrt (+ (sq dx) (sq dy))))
(define (sq x) (* x x))
(define (dist x1 y1 x2 y2)
(let ((dx (- x2 x1))
(dy (- y2 y1)))
(sqrt (+ (sq dx) (sq dy)))))
(define (area x1 y1 x2 y2 x3 y3)
(let ((a (dist x1 y1 x2 y2))
(b (dist x2 y2 x3 y3))
(c (dist x3 y3 x1 y1)))
(let ((p (/ (+ a b c) 2)))
(sqrt (* p (- p a) (- p b) (- p c))))))
(define (area x1 y1 x2 y2 x3 y3)
(let* ((a (dist x1 y1 x2 y2))
(b (dist x2 y2 x3 y3))
(c (dist x3 y3 x1 y1))
(p (/ (+ a b c) 2)))
(sqrt (* p (- p a) (- p b) (- p c)))))
(define (pow x n)
(cond ((= n 0) 1)
((< n 0) (/ 1 (pow x (- n))))
(else (* x (pow x (- n 1))))))
(define (qpow x n)
(define (sqr x) (* x x))
(cond ((= n 0) 1)
((< n 0) (/ 1 (qpow x (- n))))
((even? n) (sqr (qpow x (quotient n 2))))
(else (* x (qpow x (- n 1))))))
(define (fib n)
(if (<= n 1) n
(+ (fib (- n 1)) (fib (- n 2)))))
(define (fib n)
(define (iter i fi fi-1)
(if (= i n) fi
(iter (+ i 1) (+ fi fi-1) fi)))
(if (= n 0) 0
(iter 1 1 0)))