Scheme

No description

fork
loading
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(/ (+ 5 4 (- 2 (- 3 ( + 6 ( / 4 5 )))))
   (* 3 (- 6 2)(- 2 7))
  )

; 1.3 takes 3 numbers, return sum of squares of larger numbers


(define (square x) (* x x)
)   

(define (bigger x y)(cond ((> x y ) x)
                          ((> y x) y)
                    )
)

(define (squares a b c)(+
                         (square (bigger a b))
                         (square (bigger a c))
                       )
)

(squares 5 7 8)

; 1.7

(define guess 1)

(define (average x y)
 (/ (+ x y) 2))

(define (abs x)
 (if (< x 0)
 (- x)
 x))

(define (good-enough? guess x)
 (< (abs (- guess (improve guess x)))
    (* guess 0.001)))

(define (improve guess x)
 (average guess (/ x guess)))

(define (sqrt-iter guess x)
 (if (good-enough? guess x)
 guess
 (sqrt-iter (improve guess x)
 x)))

(define (sqrt x)
 (sqrt-iter 1.0 x))
(sqrt 2)

; 1.8 
; (((x/ y^2) +2y)  /3)

(define (improve-cube guess x)
  (/(+
      (/ x (square guess)) 
      (* guess 2)
    )
    3)
)

(define (cube-good? guess x)
 (< (abs (- guess (improve-cube guess x)))
    (* guess 0.001)))

(define (cube-iter guess x)
 (if (cube-good? guess x)
 guess       
 (cube-iter (improve-cube guess x)
 x)))

(define (cubert x)
 (cube-iter 1.0 x))
(cubert 5743895729340)

; 1. 11 compute f(n) recursively and iteratively

(define (g n) 
  (cond ((< n 3) n)
      (else (+
             (g (- n 1))
             (* 2 (g (- n 2)))
             (* 3 (g (- n 3)))
            )
      )
  )
)

(g 5)

; iterative 

(define (f n) 
   (define (iter a b c count) 
     (if (= count 0) 
      a 
       (iter b c (+ c (* 2 b) (* 3 a)) (- count 1)))
    ) 
    (iter 0 1 2 n)
) 


(define (f n) 
    (if (< n 3) n)
    (define (iter a b c count)
      (- count 3)
      (if (= count 0) 
        a 
        (
          (define (temp a))
          iter temp (* a 2) (* 2 c) (- count 1)
        )
      )
    ) 
    (iter 2 2 0)
) 
 
 (f 5)
BiwaScheme Interpreter version 0.6.4 Copyright (C) 2007-2014 Yutaka HARA and the BiwaScheme team