-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverview.scm
123 lines (89 loc) · 2.24 KB
/
overview.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
123
; OVERVIEW
; Setup environment
; LISP overview
; https://github.com/BluePat/SICP/blob/master/1.%20Building%20Abstractions%20with%20Procedures/1.0%20Introduction.md#computational-process
; SICP overview
; BASIC FORM
; Notes on basic syntax:
; https://github.com/BluePat/SICP/blob/master/1.%20Building%20Abstractions%20with%20Procedures/1.1%20The%20Elements%20of%20Programming.md
; (operator operand1 operand2 operand3 ... operandN)
(+ 1 2 3 4 5)
; SPECIAL FORMS
; Link name with primitive expression
(define one 1)
(define plus +)
; Link name with procedure
(define (plus-one x)
(+ x 1))
; Exercise:
; Define square procedure (square x)
; Define sum of squares procedure (sum-of-squares x y)
; Conditions
(define (abs x)
; (cond (<p1> <e1>) (<p2> <e2>))
; (<predicate> <expression>) - special form called `clause`
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
; Ackermann's function
; https://en.wikipedia.org/wiki/Ackermann_function
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1) (A x (- y 1))))))
; Exercise:
; Define a procedure that takes three numbers as arguments
; returns the sum of the squares of the two larger numbers.
; if-else
(define (add-one-to-odd x)
(if (odd? x)
(+ x 1)
x))
(define (add-if-first-odd x y)
((if (odd? x) + -) x))
; Exercise 1.11 - page 53
; RECURSIVE vs ITERATIVE processes
#|
RECURSIVE PROCESS
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
(+ 3 2)
(inc (+ (dec 3) 2))
(inc (+ 2 2))
(inc (inc (+ (dec 2) 2)))
(inc (inc (+ 1 2)))
(inc (inc (inc (+ (dec 1) 2))))
(inc (inc (inc (+ 0 2)))))
(inc (inc (inc 2)))
(inc (inc 3))
(inc 4)
5
ITERATIVE PROCESS
(define (+ a b)
(if (= a 0)
b
(+ (dec a) (inc b))))
(+ 3 2)
(+ (dec 3) (inc 2))
(+ 2 3)
(+ (dec 2) (inc 3))
(+ 1 4)
(+ (dec 1) (inc 4))
(+ 0 5)
5
Note: This can be easily forseen in the last call
|#
; Exercise factorial - recursive and iterative
; Exercise Fibbonacci - recursive and iterative
; COLLECTIONS
; Pairs
(define my-pair (cons 1 2))
; CAR x CDR - page 115
; Data objects constructed from pairs are called list-structured data
; box-and-pointer notation - page 132
; abstraction barrier - page 119
; List
(define my-list (list 1 2 3 4 5))