* 1.1 The Elements of Programming
the programming language serves as a framework for organizing ideas about processes

to combine simple ideas into complex ones:

 - primitive expressions
 - means of combination
 - means of abstraction

procedures, data

** 1.1.1 Expressions

with "BEGIN/END_SRC", cool interactive lisp interpreter courtesy of org-mode:

#+BEGIN_SRC scheme

  486

#+END_SRC

#+RESULTS:
: 486

plan to use this for the majority of the examples 

 - combination :: list of exps in parens (+ 3 4)
 - operator :: first elem in a comb
 - operands :: all other elems
 - arguments :: vals of the operands

 - prefix notation :: putting operator first
 - nesting :: combs of combs
 - pretty printing :: making everything look nice n tidy
 - read-eval-print-loop :: repl

#+BEGIN_SRC scheme

  (+ (* 3
	(+ (* 2 4)
	   (+ 3 5)))
     (+ (- 10 7)
	6))

#+END_SRC

#+RESULTS:
: 57

isn't she pretty???

** 1.1.2 Naming and the Environment

variables name an object that has a value

#+BEGIN_SRC scheme

  (define size 2)
  size

#+END_SRC

#+RESULTS:
: 2

taking the circumference of a circle: elementary math!

#+BEGIN_SRC scheme

(define pi 3.14159)
(define radius 10)
(define circumference (* 2 pi radius))

circumference

#+END_SRC

#+RESULTS:
: 62.8318

DEFINE is our language's simplest means of abstraction
"our language"

** 1.1.3 Evaluating Combinations

to eval a comb (RECURSION, BITCH):

 1. eval subexps of comb
 2. apply the operator to the operands

to eval primitives:
 - numbers are numbers
 - built-in operators do what they do
 - names are what they point to

 - tree accumulation :: percolate values upward
 - special form :: an exception to the general eval rules
 - recursion :: recursion

(define g 6) is a special form

** 1.1.4 Compound Procedures
essential powers of a programming language:
 -> numbers & arithmetic are primitive
 -> nesting allows combining
 -> definitions allow abstraction

Procedure Definitons: giving a name to a compound expression

#+BEGIN_SRC scheme :session sq :noweb yes

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

#+END_SRC

#+RESULTS:
: 25

procedures can be defined in terms of other procs

#+BEGIN_SRC scheme :session sq

  (define (sum-of-squares x y)
    (+ (square x) (square y)))
  (sum-of-squares 6 7)

#+END_SRC

#+RESULTS:
: 85

adding a third layer:

#+BEGIN_SRC scheme :session sq

  (define (f a)
    (sum-of-squares (+ a 1) (* a 2)))
  (f 5)

#+END_SRC

#+RESULTS:
: 136

** 1.1.5 The Substitution Model for Procedure Application
to eval a comp proc (assuming primitive built-in):
  -> eval the body with params replaced by args

 - substitution model (applicative-order) :: 
   1. (f 5)
   2. (sum-of-squares (+ a 1) (* a 2))
   3. (sum-of-squares (+ 5 1) (* 5 2))
   4. (sum-of-squares 6 10)
   5. (+ (square 6) (square 10))
   6. (+ (* 6 6) (* 10 10))
   7. (+ 36 100)
   8. 136
   

substitution is a mental model, not how it really works
simplified, incomplete, will be refined as we continue

lisp does use applicative order, compared to ...

 - normal-order evaluation ::
   1. (f 5)
   2. (sum-of-squares (+ 5 1) (* 5 2))
   3. (+ (square (+ 5 1))  (square (* 5 2)))
   4. (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
   5. (+ (* 6 6) (* 10 10))
   6. (+ 36 100)
   7. 136

** 1.1.6 Conditional Expressions and Predicates

          -- > x   if x > 0
        /
abs(x)  ---- > 0   if x = 0
        \
          -- > -x  if x < 0


 - case analysis :: branching logic
 - predicate :: evals to bool


conditional

 #+BEGIN_SRC scheme

   (define (abs x)
     (cond ((> x 0) x)
	   ((= x 0) 0)
	   ((< x 0) (- x))))

 #+END_SRC

 (cond ((predicate) (consequent-expression)))
       \ . . . . . . . . . . . . . . . . . /
                 this is a clause


if

#+BEGIN_SRC scheme

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

#+END_SRC

(if (predicate) (consequent) (alternative))
(and (expression) ... (exp))
(or (exp) ... (exp))
(not (exp))

and, or are special procedures


#+BEGIN_SRC scheme

  (and (> x 5) (< x 10))

  (define (>= x y)
    (or (> x y) (= x y)))
  (define (>= x y)
    (not (< x y)))

#+END_SRC

** 1.1.8 Procedures as Black-Box Abstractions
sqrt-iter is recursive; defined in terms of itself.
computing sqrt breaks up naturally into subproblems.

                                                  -> square
                                                / 
                            -- >  good-enough - 
                          /                     \
sqrt ---- > sqrt-iter --                          -> abs
                          \
                            -- >  improve    ---->   average

a proc should supress this detail. users should not have
to know about what is going on inside of sqrt.

 - local names :: var names chosen by proc's author are irrelevant
 - bound variable :: formal param of a proc
 - free variable :: unbound
 - scope :: set of exps for which a binding gives a name

a procedure's formal params bind vars to its body's scope

new! block structure:

#+BEGIN_SRC scheme :session :noweb yes
  (define (average x y)
    (/ (+ x y)
       2))
  (define (square x)
    (* x x))

  (define (sqrt x)
    (define (good-enough? guess x)
      (< (abs (- (square guess) x)) 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)))
    (sqrt-iter 1.0 x))

  (sqrt 36)
#+END_SRC

#+RESULTS:
: 6.000000005333189

"we will use block structure extensively" (─‿‿─) 
THE IDEA OF BLOCK STRUCTURE ORIGINATED WITH ALGOL-60
