SICP 1.1
Key Concepts #
- Environments, Syntax, Programming Languages v. Natural Languages, Applicative Order of Operations
Environments #
- Understanding meaning is a matter of combining primitives with context.
Syntax #
- Lisp programmers tend to eschew syntactic sugar in favor of building their own syntax.
- Procedures are noticeably more composable than other languages I’ve used previously.
- There’s little difference between user-defined and language-defined procedures
Comparison between programming and natural languages #
- We can define procedures (verbs) and use variables (pronouns) to build higher-level abstractions.
Operator Evaluation Order #
- We tend to use a substitution model when thinking about how interpreters evaluate operations.
- Often, this model doesn’t match how an interpreter functions
- In a lazy model, interpreters don’t evaluate the result of operations until the operands were needed
- In our substitution model, we combine results of operations from the bottom up.
- I struggled to understand this until I realized that the difference is how greedily we get results of operations.
- In the lazy model, we’re not greedy about getting results, whereas in the substitution modles, we evaluation operation results ASAP
- normal-order = lazy; applicative-order = substitution;
- Haskell uses normal-order evaluation, while Scheme uses applicative-order evaluation.
Conditional Expressions / Predicates #
Exercise 1.1 #
Q: Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.
Expression: 10
Result: 10
Expression: (+ 5 3 4)
Result: 12
Expression: (- 9 1)
Result: 8
Expression: (/ 6 2)
Result: 3
Expression: (+ (* 2 4) (- 4 6))
Result: 6
Expression: (define a 3)
Result: a?
Expression: (define b (+ a 1))
Result: b
Expresion: (+ a b (* a b))
Result: 19
Expression: (= a b)
Result: #f
Expression: (if (and (> b a) (< b (* a b)))
b
a)
Result: 4 (b)
Expression: (cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
Result: 16
Expression: (+ 2 (if (> b a) b a))
Result: 6
Expression:
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
Result: 16
Exercise 1.2 #
Q: Translate the following expression into prefix form
5 + 1/2 + (2 - (3 - (6 + 1/5)))/(3(6 - 2)(2 - 7)
A:
(/ (+ 5
(/ 1 5)
(- 2
(- 3
(+ 6 (/ 1 5)))))
(* 3
(- 6 2)
(- 2 7)))
Exercise 1.3 #
Q: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
A:
(define (sum-of-larger-squares a b c)
(cond ((and (> a c) (> b c)) (+ (sum-of-squares a b)))
((and (> b a) (> c a)) (+ (sum-of-squares b c)))
(else (+ (sum-of-squares a c)))))
Exercise 1.4 #
Q: Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
A: This function will return a - b if b is greater than 0 and a + b otherwise. This will always result in a value that is greater than a being returned. Hence, the name a-plus-abs-b.
Exercise 1.5 #
Q: Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
Then he evaluates the expression
(test 0 (p))
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)
A: With applicative-order evaluation, the this test will return the value of p, which will result in an infinitely recursive loop. With normal-order evaluation, we’ll return 0, since we don’t need the value of p.