9-October-2002cse413-05-MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Programming Languages and Paradigms
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Variables Six properties: Binding times of properties:
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
Chapter 2 Writing Simple Programs
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Languages and Paradigms Imperative Programming.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
David Stotts Computer Science Department UNC Chapel Hill.
Bordoloi and Bock Control Structures: Iterative Control.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
JavaScript, Fourth Edition
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
1 Lecture 14: Assignment and the Environment Model (EM)
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
21-October-2002cse IO © 2002 University of Washington1 Input / Output CSE 413, Autumn 2002 Programming Languages
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Operational Semantics of Scheme
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
JavaScript: Functions.
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.
The Metacircular Evaluator
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
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.
6.001 SICP Environment model
CSE341: Programming Languages Lecture 12 Equivalence
Bindings, Scope, and Extent
6.001 SICP Further Variations on a Scheme
Explicit Application of Procedures, and Explicit Evaluation
Streams, Delayed Evaluation and a Normal Order Interpreter
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
Flow of Control.
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

9-October-2002cse MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages

9-October-2002cse MoreLambda © 2002 University of Washington2 Readings and References Reading »Section 1.3, Structure and Interpretation of Computer Programs, by Abelson, Sussman, and Sussman, but you've already read this, right? Other References

9-October-2002cse MoreLambda © 2002 University of Washington3 Procedures as unnamed blobs With lambda, we've separated the body of the procedure from any particular name for the procedure Procedures are objects like any other, and can be handed around from procedure to procedure as arguments, return values, etc Procedures can be defined and applied without ever getting a name assigned to them

9-October-2002cse MoreLambda © 2002 University of Washington4 a numeric interval x = [0:.1:1]; y = sin(x); plot(x,y) interval h last interval last x

9-October-2002cse MoreLambda © 2002 University of Washington5 calculate-h ; define a function to calculate an ; interval size (b-a)/n (define calculate-h (lambda (a b n) (/ (- b a) n))) ; try it out on [0,1] (calculate-h ) 1 — 10 define a function body... … and bind it to the name calculate-h apply the function to some arguments

9-October-2002cse MoreLambda © 2002 University of Washington6 anonymous calculate-h ; do the same thing without naming the function ((lambda (a b n) (/ (- b a) n)) ) define a function body... … and apply it to some arguments 1 — 10

9-October-2002cse MoreLambda © 2002 University of Washington7 calculate last-x ; define a function that figures out what the beginning ; of the last interval is ; calculate a+(h*(n-1)) directly (define (last-x1 a b n) (+ a (* (- n 1) (/ (- b a) n))))

9-October-2002cse MoreLambda © 2002 University of Washington8 last-x using a helper function ; calculate a+(k*h) using a simple function, and ; pre-calculate k and h to pass to the function (define (last-x2 a b n) (define (use-kh k h) (+ a (* k h ))) (use-kh (- n 1) (/ (- b a) n))) define a function body and bind it to the name use-kh apply use-kh to some arguments

9-October-2002cse MoreLambda © 2002 University of Washington9 last-x using anonymous helper function ; calculate a+(k*h) using an anonymous function (define (last-x3 a b n) ((lambda (k h) (+ a (* k h ))) (- n 1) (/ (- b a) n))) define a function body... … and apply it to some arguments k h

9-October-2002cse MoreLambda © 2002 University of Washington10 last-x with concealed anonymous function ; hide the use of the anonymous function by using let (define (last-x4 a b n) (let ((h (/ (- b a) n)) (k (- n 1))) (+ a (* k h )))) bind some values to some names... … and use those names in the body of the let

9-October-2002cse MoreLambda © 2002 University of Washington11 Special form let (let ((  var 1   exp 1  ) (  var 2   exp 2  ))  body  ) When the let is evaluated, each expression exp i is evaluated and the resulting value is associated with the related name var i, then the body is evaluated. There is no order implied in the evaluation of exp i This is exactly the same as parameter evaluation before a procedure call »This is parameter evaluation before a procedure call

9-October-2002cse MoreLambda © 2002 University of Washington12 scope and let ; an example in scoping with let (define x 2) (let ((x 3) (y (+ x 2))) (* x y)) ((lambda (x y) (* x y)) 3 (+ x 2)) scope of the local x and y the parameter values are calculated outside the scope of the parameter variables in the procedure this let and this lambda are equivalent

9-October-2002cse MoreLambda © 2002 University of Washington13 nesting let s lets us get x ; nested lets and let* (define x 2) (let ((x 3)) (let ((y (+ x 2))) (* x y))) (let* ((x 3) (y (+ x 2))) (* x y)) is this x referenced anywhere?

9-October-2002cse MoreLambda © 2002 University of Washington14 Special form let* (let* ((  var 1   exp 1  ) (  var 2   exp 2  ))  body  ) When the let* is evaluated, each expression exp i is evaluated in turn and the resulting value is associated with the related name var i, then the body is evaluated. The exp i are evaluated in left to right order »each binding indicated by (  var i   exp i  ) is part of the environment for (  var i+1   exp i+1  ) and following »This is exactly equivalent to nesting the let statements

9-October-2002cse MoreLambda © 2002 University of Washington15 an iterator with parameter h ; show all the x values on the interval (define (show-x1 a b n) (define (iter h count) (if (> count n) (newline) (begin (display (+ a (* h count))) (display " ") (iter h (+ count 1))))) (iter (/ (- b a) n) 0))

9-October-2002cse MoreLambda © 2002 University of Washington16 h defined in enclosing scope ; show all the x values on the interval ; using let (define (show-x2 a b n) (let ((h (/ (- b a) n))) (define (iter count) (if (> count n) (newline) (begin (display (+ a (* h count))) (display " ") (iter (+ count 1))))) (iter 0)))

9-October-2002cse MoreLambda © 2002 University of Washington17 Special form begin (begin  exp 1   exp 2    exp n  ) Evaluate the exp i in sequence from left to right The value returned by the entire begin expression is the value of exp n Best used to sequence side effects like I/O »for example displaying each of the x values in show-x There is implicit sequencing in the body of a lambda procedure or a let but we generally don't use it »the procedure returns the value of the last exp i, so the body of most of our procedures consists of one expression only

9-October-2002cse MoreLambda © 2002 University of Washington18 sequencing with begin ; show all the x values on the interval ; using let (define (show-x2 a b n) (let ((h (/ (- b a) n))) (define (iter count) (if (> count n) (newline) (begin (display (+ a (* h count))) (display " ") (iter (+ count 1))))) (iter 0))) special form: if (if exp tx fx)

9-October-2002cse MoreLambda © 2002 University of Washington19 show-x Welcome to DrScheme, version 201. Language: Standard (R5RS). > (show-x ) 0 1/10 1/5 3/10 2/5 1/2 3/5 7/10 4/5 9/10 1 >