Introduction to the Lab

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Original material by Eric Grimson
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
This Lecture Substitution model
CS220 Programming Principles
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
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.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
6.001 SICP Environment model
Lecture 24: Metalinguistics CS200: Computer Science
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Extended Introduction to Computer Science
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
6.001 SICP Explicit-control evaluator
Material in the textbook Sections to 1.2.1
6.001 SICP Explicit-control evaluator
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
6.001: Structure and Interpretation of Computer Programs
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Good programming practices
Presentation transcript:

Introduction to the 6.001 Lab Sunday at 4:00 Rules of the lab Using the lab Getting the most out of the lab Meet the LA’s 34-501 5/7/2019 6.001 SICP

Last lecture Basics of Scheme Self-evaluating expressions Names Define Rules for evaluation 5/7/2019 6.001 SICP

This lecture Adding procedures and procedural abstractions Using procedures to capture processes 5/7/2019 6.001 SICP

Language elements -- abstractions Need to capture ways of doing things – use procedures parameters (lambda (x) (* x x)) body To process something multiply it by itself Special form – creates a procedure and returns it as value 5/7/2019 6.001 SICP

Language elements -- abstractions Use this anywhere you would use a procedure ((lambda (x) (* x x)) 5) 5/7/2019 6.001 SICP

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 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. 5/7/2019 6.001 SICP

Language elements -- abstractions Use this anywhere you would use a procedure ((lambda (x) (* x x)) 5) (* 5 5) 25 Can give it a name (define square (lambda (x) (* x x))) (square 5)  25 Rumplestiltskin effect! 5/7/2019 6.001 SICP

Lambda: making new procedures printed representation of value expression (lambda (x) (* x x)) #[compound-procedure 9] print eval lambda-rule A compound proc that squares its argument value 5/7/2019 6.001 SICP

Interaction of define and lambda (lambda (x) (* x x)) ==> #[compound-procedure 9] (define square (lambda (x) (* x x))) ==> undef (square 4) ==> 16 ((lambda (x) (* x x)) 4) ==> 16 (define (square x) (* x x)) ==> undef This is a convenient shorthand (called “syntactic sugar”) for 2 above – this is a use of lambda! 5/7/2019 6.001 SICP

Lambda special form lambda syntax (lambda (x y) (/ (+ x y) 2)) 1st operand position: the parameter list (x y) a list of names (perhaps empty) determines the number of operands required 2nd operand position: the body (/ (+ x y) 2) may be any expression not evaluated when the lambda is evaluated evaluated when the procedure is applied semantics of lambda: 5/7/2019 6.001 SICP

THE VALUE OF A LAMBDA EXPRESSION IS A PROCEDURE 5/7/2019 6.001 SICP

Using procedures to describe processes How can we use the idea of a procedure to capture a computational process? 5/7/2019 6.001 SICP

What does a procedure describe? Capturing a common pattern (* 3 3) (* 25 25) (* foobar foobar) Common pattern to capture (lambda (x) (* x x) ) Name for thing that changes 5/7/2019 6.001 SICP

Modularity of common patterns Here is a common pattern: (sqrt (+ (* 3 3) (* 4 4))) (sqrt (+ (* 9 9) (* 16 16))) (sqrt (+ (* 4 4) (* 4 4)) Here is one way to capture this pattern: (define pythagoras (lambda (x y) (sqrt (+ (* x x) (* y y))))) 5/7/2019 6.001 SICP

Modularity of common patterns Here is a common pattern: (sqrt (+ (* 3 3) (* 4 4))) (sqrt (+ (* 9 9) (* 16 16))) (sqrt (+ (* 4 4) (* 4 4))) But here is a cleaner way of capturing patterns: (define square (lambda (x) (* x x))) (define pythagoras (lambda (x y) (sqrt (+ (square x) (square y))))) 5/7/2019 6.001 SICP

Why? Breaking computation into modules that capture commonality Enables reuse in other places (e.g. square) Isolates details of computation within a procedure from use of the procedure May be many ways to divide up (define square (lambda (x) (* x x))) (define sum-squares (lambda (x y) (+ (square x) (square y)))) (define pythagoras (lambda (y x) (sqrt (sum-squares y x)))) 5/7/2019 6.001 SICP

Abstracting the process Stages in capturing common patterns of computation Identify modules or stages of process Capture each module within a procedural abstraction Construct a procedure to control the interactions between the modules Repeat the process within each module as necessary 5/7/2019 6.001 SICP

A more complex example Remember our method for finding sqrts To find the square root of X Make a guess, called G If G is close enough, stop Else make a new guess by averaging G and X/G 5/7/2019 6.001 SICP

The stages of “SQRT” When is something “close enough” How do we create a new guess How to we control the process of using the new guess in place of the old one 5/7/2019 6.001 SICP

Procedural abstractions For “close enough”: (define close-enuf? (lambda (guess x) (< (abs (- (square guess) x)) 0.001))) Note use of procedural abstraction! 5/7/2019 6.001 SICP

Procedural abstractions For “improve”: (define average (lambda (a b) (/ (+ a b) 2))) (define improve (lambda (guess x) (average guess (/ x guess)))) 5/7/2019 6.001 SICP

Why this modularity? “Average” is something we are likely to want in other computations, so only need to create once Abstraction lets us separate implementation details from use E.g. could redefine as No other changes needed to procedures that use average Also note that variables (or parameters) are internal to procedure – cannot be referred to by name outside of scope of lambda (define average (lambda (x y) (* (+ x y) 0.5))) 5/7/2019 6.001 SICP

Controlling the process Basic idea: Given X, G, want (improve G X) as new guess Need to make a decision – for this need a new special form (if <predicate> <consequence> <alternative>) 5/7/2019 6.001 SICP

The IF special form (if <predicate> <consequence> <alternative>) Evaluator first evaluates the <predicate> expression. If it evaluates to a TRUE value, then the evaluator evaluates and returns the value of the <consequence> expression. Otherwise, it evaluates and returns the value of the <alternative> expression. Why must this be a special form? 5/7/2019 6.001 SICP

Controlling the process Basic idea: Given X, G, want (improve G X) as new guess Need to make a decision – for this need a new special form (if <predicate> <consequence> <alternative>) So heart of process should be: (if (close-enuf? G X) G (improve G X) ) But somehow we want to use the value returned by “improving” things as the new guess, and repeat the process 5/7/2019 6.001 SICP

Controlling the process Basic idea: Given X, G, want (improve G X) as new guess Need to make a decision – for this need a new special form (if <predicate> <consequence> <alternative>) So heart of process should be: (define sqrt-loop (lambda G X) (if (close-enuf? G X) G (sqrt-loop (improve G X) X ) But somehow we want to use the value returned by “improving” things as the new guess, and repeat the process Call process sqrt-loop and reuse it! 5/7/2019 6.001 SICP

Putting it together Then we can create our procedure, by simply starting with some initial guess: (define sqrt (lambda (x) (sqrt-loop 1.0 x))) 5/7/2019 6.001 SICP

Checking that it does the “right thing” Next lecture, we will see a formal way of tracing evolution of evaluation process For now, just walk through basic steps (sqrt 2) (sqrt-loop 1.0 2) (if (close-enuf? 1.0 2) … …) (sqrt-loop (improve 1.0 2) 2) This is just like a normal combination (sqrt-loop 1.5 2) (if (close-enuf? 1.5 2) … …) (sqrt-loop 1.4166666 2) And so on… 5/7/2019 6.001 SICP

Abstracting the process Stages in capturing common patterns of computation Identify modules or stages of process Capture each module within a procedural abstraction Construct a procedure to control the interactions between the modules Repeat the process within each module as necessary 5/7/2019 6.001 SICP

Summarizing Scheme Primitives Numbers Strings 1, -2.5, 3.67e25 Booleans Built in procedures Means of Combination (procedure argument1 argument2 … argumentn) Means of Abstraction Lambda Define Other forms if 1, -2.5, 3.67e25 Creates a loop in system – allows abstraction of name for object -- Names *, +, -, /, =, >, <, Enables means of combination . Create a procedure . Create names . Control order of evaluation 5/7/2019 6.001 SICP