Created:
Last modified:
Programming
Reading through the first few subchapters of Structure and Interpretation of Computer Programs.
1.1 The Elements of Programming
I'm confirming already that at least some of this learning is going to be putting names to ideas and
concepts I use all the time, but don't always know the correct terms for. For example, something as simple
as primatives, a languages simplest entities. And I will admit, I feel silly writing this,
and I imagine I'll feel silly about a lot of things I'll write in this series, because this feels like a no
brainers. However, it's not like I don't know was the basic pieces are of a language I use, for example in
Javascript I believe this would be things like a string or object, I just wouldn't think to call them primatives. Therefore I also
mightn't realise what someone else is talking about when they use this term, though the name makes it quite
obvious, I think.
Evaluation of a Procedure
Normal-order evaluation is where the language interpreter expands fully all arguments in a procedure and then reduces them, where applicative-order evaluation evaluates all arguments first and then applies them. The latter process reduces duplication.
1.1 Elements of Programming Exercises - For my information only
Exercises 1.1
- 10
- 12
- 8
- 3
- 6
- 19
- false
- 4
- 16
- 6
- 16
Exercise 1.2
5 + 4 + (2-(3-(6 + 1/5)))
/ 3(6-2)(2-7)(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 1 5))))) (* 3 (- 6 2)(- 2 7)))
Exercise 1.3
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers
(define (sumLargestSquares a b c))
(define (square num)(* num num))
(define (plusSqr num1 num2)
(+ (square num1) (square num2)))
cond ((= a b c) (plusSqr a a)
(if (a > b) (plusSqr a (b > c) b c))
(if (b > a) (plusSqr b (a > c) a c))
(if (c > a) (plusSqr c (a > b) a b))
)
Exercise 1.4
Explain the following procedure
(define (a-plus-abs-b a b) ((if (> b 0) + -) a b))If the value of b is greater than 0 the procedure will print the sum of a and b, otherwise it will print the subtraction of b from a.
Exercise 1.5
Evaluating applicative-order evaluation vs normal-order evaluationWith applicative-order, 0 will be printed as arguments are only evaluated when needed. With normal-order however, the interpreter might throw an error or be stuck in a loop as it tries to evaluate the value of "p" which seems to be a procedure which calls itself infinitely. Unsure if this is correct.
Exercise 1.6
Non recursive. When the initial test fails, it doesn't run the passed procedure, it looks for the next
predicate, but there isn't one. So it ends and returns undefined
Exercise 1.7
(define (end-test new-guess old-guess)
(
< (abs new-guess old-guess) 0.001)
)
Exercise 1.8
(define find-cube-root guess x)(/ (+ (/ x (* guess guess)) (* guess 2)) 3)
1.2 Procedures and the Processes They Generate
Exercise 1.9
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
(+ 4 6)
(inc (+ (dec a) b))
(inc (+ (dec 4) 6))
(inc (+ (- 4 1) 6))
(inc (+ 3
6))
(inc 9)
(+ 9 1)
10
(define (+ a b)
(if (= a 0)
b
(+ (dec a) (inc b))
)
)
(+ 4
6)
(+ (dec 4) (inc 6))
(+ (- 4 1) (+ 6 1))
(+ 3 7)
10
First procedure is recursive, second is iterative
Exercise 1.10
- 18
- 4
- 2
- 2n
- (n === 1) ? 2 : 2n
- (n <= 2) ? 2 : 2n
Actually going to look at switching to the
Glossary
- Predicate
Procedures and expressions which are evaluated to return boolean - Lexical Scoping
Variables defined within a procedure are accessible within that procedure, but not outside. These variables do not need to be passed to other nested procedures, and instead can be access in that locally scoped environment. - Induction
4. Logic
- also called Baconian method. Any form of reasoning in which the conclusion, though supported by the premises, does not follow from them necessarily
- the process of estimating the validity of observations of part of a class of facts as evidence for a proposition about the whole class
- a conclusion reached by this process