What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018 מבוא מורחב
Declarative Knowledge “What is true” knowledge 12/25/2018 מבוא מורחב
Imperative Knowledge “How to” knowledge To find an approximation of square root of x: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 12/25/2018 מבוא מורחב
“How to” knowledge Process – series of specific, mechanical steps for deducing information, based on simpler data and set of operations Procedure – particular way of describing the steps a process will evolve through Need a language for description: vocabulary rules for connecting elements – syntax rules for assigning meaning to constructs – semantics Using language of procedures to solve problems 12/25/2018 מבוא מורחב
Scheme Basics Rules for Scheme Legal expressions have rules for constructing from simpler pieces (Almost) every expression has a value, which is “returned” when an expression is “evaluated”. Every value has a type. 12/25/2018 מבוא מורחב
Language Elements Primitives Means of combination Means of abstraction 12/25/2018 מבוא מורחב
Language elements -- primitives Numbers – self-evaluating rule 23 23 -36 -36 Names for built-in procedures +, *, /, -, =, … What is the value of such an expression? + [#procedure …] Evaluating by looking up value associated with name 12/25/2018 מבוא מורחב
Language elements – combinations How do we create expressions using these procedures? (+ 2 3) Close paren Open paren Expression whose value is a procedure Other expressions Evaluate by getting values of subexpressions, then apply operator to values of arguments 12/25/2018 מבוא מורחב
Language elements - combinations Can use nested combinations – just apply rules recursively (+ (* 2 3) 4) 10 (* (+ 3 4) (- 8 2)) 42 12/25/2018 מבוא מורחב
Language elements -- abstractions In order to abstract an expression, need way to give it a name (define score 23) This is a special form Does not evaluate second expression Rather, it pairs that name with the value of the third expression in an environment Return value is unspecified 12/25/2018 מבוא מורחב
Language elements -- abstractions To get the value of a name, just look up pairing in environment score 23 Note that we already did this for +, *, … (define total (+ 12 13)) (* 100 (/ score total)) 92 can create a complex thing, name it, treat it as primitive 12/25/2018 מבוא מורחב
Scheme Basics Rules for evaluation If self-evaluating, return value. If a name, return value associated with name in environment. If a special form, do something special. If a combination, then a. Evaluate all of the subexpressions of combination (in any order) b. apply the operator to the values of the operands (arguments) and return result 12/25/2018 מבוא מורחב
Capturing common patterns Here are some common patterns (* 5 5) (* 23 23) (* score score) (* x x) How do we generalize (e.g. the last expression)? 12/25/2018 מבוא מורחב
Language elements -- abstractions Need to capture ways of doing things – use procedures (lambda (x) (* x x)) parameters body To process something multiply it by itself Special form – creates a procedure and returns it as value 12/25/2018 מבוא מורחב
Language elements -- abstractions Use this anywhere you would use a proceduure ((lambda (x) (* x x)) 5) 12/25/2018 מבוא מורחב
Scheme Basics Rules for evaluation Rules for application If self-evaluating, return value. If a name, return value associated with name in environment. If a special form, do something special. If a combination, then a. Evaluate all of the subexpressionss of combination (in any order) b. apply the operator to the values of the operands (arguments) and return result Rules for application If procedure is primitive procedure, just do it. If procedure is a compound procedure, then: evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument value. 12/25/2018 מבוא מורחב
Language elements -- abstractions Use this anywhere you would use a proceduure ((lambda (x) (* x x)) 5) (* 5 5) 25 Can give it a name (define square (lambda (x) (* x x))) (square 5) 25 12/25/2018 מבוא מורחב
Taxonomy of Expressions Primitive Expressions Constants (self-evaluating) Numerals, e.g.17 == Sch-Num 7 Strings, e.g. “hello” == Sch-String hello Booleans, e.g. #f, #t == Sch-Bool Names E.g. + == primitive procedure E.g. x == variable created by define or procedure application Compound Expressions Combinations: (<operator> <operand> <operand> …) Special forms: Define: (define <name> <exp>) Lambda: (lambda (<formal1> <formal2> …) <body>) If: (if <predicate> <consequent> <alternative>) …. More to come 12/25/2018 מבוא מורחב