Procedural Abstraction and Recursion
Total Page:16
File Type:pdf, Size:1020Kb
Class Structure Procedural abstraction and recursion 6.037 - Structure and Interpretation of Computer Programs TR, 7-9PM, through end of IAP http://web.mit.edu/alexmv/6.037/ Mike Phillips, Benjamin Barenblat, Leon Shen, Ben Vandiver, Alex Vandiver, Arthur Migdal E-mail: [email protected] Five projects: due on the 10th, 15th, 17th, 24th, and 1st. Massachusetts Institute of Technology Graded P/D/F Taking the class for credit is zero-risk! Lecture 1 E-mail list sign-up on the website http://web.mit.edu/alexmv/6.037/ Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 1 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 2 / 63 Goals of the Class Prerequisites This is not a class to teach Scheme High confusion threshold Nor really a class about programming at all Some programming clue This is a course about Computer Science A copy of Racket (Formerly PLT Scheme / DrScheme) ...which isn’t about computers http://www.racket-lang.org/ ...nor actually a science Free time This is actually a class in computation Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 3 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 4 / 63 Project 0 Some History Lisp invented in 1959 by John McCarthy (R.I.P. 2010) Scheme invented in 1975 by Guy Steele and Gerald Project 0 is out today Sussman Due on Thursday! Hardware Lisp machines, around 1978 Mail to [email protected] 6.001 first taught in 1980 Collaboration with current students is fine, as long as you note it SICP published in 1984 and 1996 R6RS in 2007 6.001 last taught in 2007 6.037 first taught in 2009 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 5 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 6 / 63 The Book (“SICP”) Key ideas Procedural and data abstraction Conventional interfaces & programming paradigms Structure and Interpretation of Computer Programs Type systems by Harold Abelson and Gerald Jay Sussman Streams http://mitpress.mit.edu/sicp/ Object-oriented programming Not required reading Metalinguistic abstraction Creating new languages Useful as study aid and reference Evaluators Roughly one lecture per chapter Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 7 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 8 / 63 Lectures Projects 1 Syntax of Scheme, procedural abstraction, and recursion 2 Data abstractions, higher order procedures, symbols, and quotation 0 Basic Scheme warm-up Thursday 1/10 3 Mutation, and the environment model 1 Higher-order procedures and symbols Tuesday 1/15 4 Interpretation and evaluation 2 Mutable objects and procedures with state Thursday 1/17 3 Meta-circular evaluator Thursday 1/24 5 Debugging 4 OOP evaluator (The Adventure Game) Friday 2/1* 6 Language design and implementation 7 Continuations, concurrency, lazy evaluation, and streams 8 6.001 in perspective, and the Lambda Calculus Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 9 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 10 / 63 Computation is Imperative Knowledge “How to” knowledge “How to” knowledge p To approximate x (Heron’s Method): Make a guess G x Improve the guess by averaging G and G Keep improving until it is good enough Could just store tons of “what is” information x = 2 G = 1 Much more useful to capture “how to” knowledge – a series of steps to be followed to deduce a value – a x = 2 G = (1+2) = 1:5 G 2 procedure. 3 4 x 4 ( 2 + 3 ) G = 3 G = 2 = 1:4166 17 24 x 24 ( 12 + 17 ) G = 17 G = 2 = 1:4142 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 11 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 12 / 63 Describing “How to” knowledge Representing basic information Numbers As floating point values In IEEE 754 format Need a language for describing processes: Stored in binary Vocabulary – basic primitives In registers Made up of bits Rules for writing compound expressions – syntax Stored in flip-flops Rules for assigning meaning to constructs – semantics Made of logic gates Implemented by transistors Rules for capturing process of evaluation – procedures In silicon wells With electrical potential Of individual electrons With mass, charge, spin, and chirality Whose mass is imparted by interaction with the Higgs field ... Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 13 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 14 / 63 Assuming a basic level of abstraction Rules for describing processes in Scheme We assume that our language provides us with a basic set of data elements: Numbers Legal expressions have rules for constructing from simpler pieces – the syntax. Characters (Almost) every expression has a value, which is “returned” when an expression is “evaluated.” Booleans Every value has a type. It also provides a basic set of operations on these primitive elements The latter two are the semantics of the language. We can then focus on using these basic elements to construct more complex processes Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 15 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 16 / 63 Language elements – primitives Language elements – primitives Self-evaluating primitives – value of expression is just object itself: Built-in procedures to manipulate primitive objects: Numbers 29, −35, 1:34, 1:2e5 Numbers +, -, *, /, >, <, >=, <=, = Strings “this is a string” “odd #$@%#$ thing number 35” Strings string-length, string=? Booleans #t, #f Booleans and, or, not Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 17 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 18 / 63 Language elements – primitives Language elements – combinations How to we create expressions using these procedures? (+ 2 3) Names for built-in procedures Open paren +, -, *, /, =,... Expression whose value is a procedure What is the value of them? Other expressions Close paren + ! #<procedure:+> This type of expression is called a combination Evaluate by looking up value associated with the name in a special table – the environment. Evaluate it by getting values of sub-expressions, then applying operator to values of arguments. You now know all there is to know about Scheme syntax! (almost) Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 19 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 20 / 63 Language elements – combinations Language elements – abstractions In order to abstract an expression, need a way to give it a name (define score 23) Note the recursive definition – can use combinations as expressions to other combinations: This is a special form (+ ( 2 3) 4) ! 10 * Does not evaluate the second expression (* (+ 3 4) (- 8 2)) ! 42 Rather, it pairs the name with the value of the third expression The return value is unspecified Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 21 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 22 / 63 Language elements – abstractions Language elements – common errors (5 + 6) => procedure application: expected procedure, given: 5; arguments were: #<procedure:+> 6 To get the value of a name, just look up pairing in the environment (define score 23) ! undefined ((+ 5 6)) score ! 23 => procedure application: expected procedure, (define total (+ 12 13)) ! undefined given: 11 (no arguments) (* 100 (/ score total)) ! 92 (* 100 (/ score totla)) => reference to undefined identifier: totla Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 23 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 24 / 63 Scheme basics Mathematical operators are just names Rules for evaluation: (+ 3 5) ! 8 If self-evaluating, return value (define fred +) ! undefined (fred 3 6) ! If a name, return value associated with name in environment 9 + is just a name If a special form, do something special If a combination, then + is bound to a value which is a procedure Evaluate all of the sub-expressions, in any order line 2 binds the name fred to that same value Apply the operator to the values of the operands and return the result Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 25 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 26 / 63 All names are names Making our own procedures (+ 3 5) ! 8 (define + *) ! undefined To capture a way of doing things, create a procedure: (+ 3 5) ! 15 (lambda (x) (* x x)) There’s nothing “special” about the operators you take for granted, either! (x) is the list of parameters Their values can be changed using define just as well (* x x) is the body Of course, this is generally a horrible idea lambda is a special form: create a procedure and returns it Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 27 / 63 Mike Phillips (MIT) Procedural abstraction and recursion Lecture 1 28 / 63 Substitution Scheme basics Rules for evaluation: Use this anywhere you would use a built-in procedure like +: If self-evaluating, return value ( (lambda (x) (* x x))5) If a name, return value associated with name in environment Substitute the value of the provided arguments into the body: If a special form, do something special. (* 5 5) If a combination, then Can also give it a name: Evaluate all of the sub-expressions, in any order (define square (lambda(x) (* x x))) Apply the operator to the values of the operands and return the result (square 5) ! 25 Rules for applying: