Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Similar documents
Class Structure. Prerequisites

Chapter 1. Fundamentals of Higher Order Programming

An introduction to Scheme

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.001 Notes: Section 6.1

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Higher-Order Procedures

6.001 Notes: Section 8.1

Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z

6.001 Notes: Section 15.1

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Introduction to Scheme

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

A Brief Introduction to Scheme (I)

CSCC24 Functional Programming Scheme Part 2

Administrivia. Simple data types

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Principles of Programming Languages

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Evaluating Scheme Expressions

Organization of Programming Languages CS3200/5200N. Lecture 11

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

A Small Interpreted Language

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 314 Principles of Programming Languages. Lecture 16

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Recap: Functions as first-class values

6.001: Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Programming

Programming Languages

Functional programming with Common Lisp

An Introduction to Scheme

Lexical vs. Dynamic Scope

Intro. Scheme Basics. scm> 5 5. scm>

An Explicit Continuation Evaluator for Scheme

6.001 Notes: Section 1.1

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Lecture 5: Lazy Evaluation and Infinite Data Structures

SCHEME AND CALCULATOR 5b

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Principles of Programming Languages

University of Massachusetts Lowell

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Functional Programming. Pure Functional Languages

6.001 Notes: Section 5.1

CS 360 Programming Languages Interpreters

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

6.001 Notes: Section 4.1

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Introduction to Functional Programming

Module 8: Local and functional abstraction

INTERPRETERS AND TAIL CALLS 9

What is AI? Knowledge-based systems in Bioinformatics, 1MB602, Definitions of AI. Acting humanly: Turing test. AI techniques. Intelligent agents

Chapter 15 Functional Programming Languages

CS 314 Principles of Programming Languages

MetacircularScheme! (a variant of lisp)

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

DRAWING ENVIRONMENT DIAGRAMS

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

(scheme-1) has lambda but NOT define

Lecture 12: Conditional Expressions and Local Binding

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CSE 505: Concepts of Programming Languages

Project 5 - The Meta-Circular Evaluator

CS 314 Principles of Programming Languages

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

LECTURE 16. Functional Programming

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Scheme Quick Reference

A Brief Introduction to Scheme (II)

CS 61A Discussion 8: Scheme. March 23, 2017

Lecture 04 FUNCTIONS AND ARRAYS

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Variables and Bindings

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lambda Calculus. Gunnar Gotshalks LC-1

Transcription:

Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Previous lecture Basics of Scheme Expressions and associated values (or syntax and semantics) Self-evaluating expressions 1, this is a string, #f Names +, *, >=, < Combinations (* (+ 1 2) 3) Define Rules for evaluation

Read-Eval-Print Visible world (+ 3 (* 4 5)) READ EVAL Internal representation for expression Value of expression Execution world PRINT Visible world 23

Summary of expressions Numbers: value is expression itself Primitive procedure names: value is pointer to internal hardware to perform operation Define : has no actual value; is used to create a binding in a table of a name and a value Names: value is looked up in table, retrieving binding Rules apply recursively

Simple examples 25 25 (+ (* 3 5) 4) 60 + [#primitive procedure ] (define foobar (* 3 5)) no value, creates binding of foobar and 15 foobar 15 (value is looked up) (define fred +) no value, creates binding (fred 3 5) 15

This lecture Adding procedures and procedural abstractions to capture processes

Language elements -- procedures 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

Language elements -- procedures Use this anywhere you would use a procedure ((lambda(x)(* x x)) 5) (* 5 5) 25 lambda exp arg

Language elements -- abstraction Use this anywhere you would use a procedure ((lambda(x)(* x x)) 5) Don t want to have to write obfuscatory code so can give the lambda a name (define square (lambda (x) (* x x))) (square 5) 25 Rumplestiltskin effect! (The power of naming things)

Scheme Basics Rules for evaluating 1. If self-evaluating, return value. 2. If a name, return value associated with name in environment. 3. If a special form, do something special. 4. 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 applying 1. If procedure is primitive procedure, just do it. 2. If procedure is a compound procedure, then: evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument value.

Interaction of define and lambda 1. (lambda (x) (* x x)) ==> #[compound-procedure 9] 2. (define square (lambda (x) (* x x))) ==> undef 3. (square 4) ==> 16 4. ((lambda (x) (* x x)) 4) ==> 16 5. (define (square x) (* x x)) ==> undef This is a convenient shorthand (called syntactic sugar ) for 2 above this is a use of lambda!

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(s) not evaluated when the lambda is evaluated evaluated when the procedure is applied value of body is value of last expression evaluated mini-quiz: (define x (lambda ()(+ 3 2))) no value x - procedure (x) - 5 semantics of lambda:

THE VALUE OF A LAMBDA EXPRESSION IS A PROCEDURE

Achieving Inner Peace (and a good grade) * The value of a lambda expression is a procedure The value of a lambda expression is a procedure *Om Mani Padme Hum

Using procedures to describe processes How can we use the idea of a procedure to capture a computational process?

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

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)))))

Modularity of common patterns Here is a common pattern: (sqrt (+ (* 3 3) (* 4 4))) (sqrt (+ (* 9 9) (* 16 16))) (sqrt (+ (* 4 4) (* 4 4))) So here is a cleaner way of capturing the pattern: (define square (lambda (x) (* x x))) (define pythagoras (lambda (x y) (sqrt (+ (square x) (square y)))))

Why? Breaking computation into modules that capture commonality Enables reuse in other places (e.g. square) Isolates (abstracts away) details of computation within a procedure from use of the procedure Useful even if used only once (i.e., a unique pattern) (define (comp x y)(/(+(* x y) 17)(+(+ x y) 4)))) (define (comp x y)(/ (prod+17 x y) (sum+4 x y)))

Why? May be many ways to divide up (define square (lambda (x) (* x x))) (define pythagoras (lambda (x y) (sqrt (+ (square x) (square y))))) (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))))

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

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 The stages of SQRT When is something close enough How do we create a new guess How do we control the process of using the new guess in place of the old one

Procedural abstractions For close enough : (define close-enuf? (lambda (guess x) (< (abs (- (square guess) x)) 0.001))) Note use of procedural abstraction!

Procedural abstractions For improve : (define average (lambda (a b) (/ (+ a b) 2))) (define improve (lambda (guess x) (average guess (/ x guess))))

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 Originally: (define average (lambda (a b) (/ (+ a b) 2))) Could redefine as (define average (lambda (x y) (* (+ x y) 0.5))) 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

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>)

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? (i.e. why not just a regular lambda procedure?)

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

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!

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)))

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

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

Summarizing Scheme Primitives Numbers Strings Booleans Built in procedures Means of Combination (procedure argument 1 argument 2 argument n ) Means of Abstraction Lambda. Define Other forms if -- Names. 1, -2.5, 3.67e25 *, +, -, /, =, >, <, Create a procedure Create names. Control order of evaluation Creates a loop in system allows abstraction of name for object