Func+on applica+ons (calls, invoca+ons)

Similar documents
CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

LISP: LISt Processing

Overview. Factorial Revisited. Iteration via Tail Recursion in Racket. An itera*ve approach to factorial. What is itera*on?

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring SOLUTIONS

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring

Overview. Iteration via Tail Recursion in Racket SOLUTIONS. Factorial Revisited. An itera*ve approach to factorial

Introduc)on To Standard ML

Overview. Iteration via Tail Recursion in Racket. Factorial Revisited. An itera*ve approach to factorial. What is itera*on?

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

Functional abstraction

Recursive List Func.ons in Racket

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Related Course Objec6ves

Local defini1ons. Func1on mul1ples- of

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

Ways to implement a language

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

FOFL and FOBS: First-Order Functions

CS2500 Exam 2 Fall 2011

Functional Programming. Pure Functional Programming

Implemen'ng OCaml in OCaml

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

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

Concepts of programming languages

SCHEME AND CALCULATOR 5b

Chapter 1. Fundamentals of Higher Order Programming

CS1 Recitation. Week 1

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

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

Introduction to Lambda Calculus. Lecture 7 CS /08/09

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

Organization of Programming Languages CS3200/5200N. Lecture 11

CSCC24 Functional Programming Scheme Part 2

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

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

Module 8: Local and functional abstraction

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

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

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

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

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

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS115 - Module 1. Cameron Morland. Fall Cameron Morland CS115 - Module 1

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Hofl, a Higher-order Functional Language. 1 An Overview of Hofl. 2 Abstractions and Function Applications

Functional Programming. Pure Functional Languages

Lecture 2: SML Basics

Functions & First Class Function Values

Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

CPSC W1 University of British Columbia

Principles of Programming Languages

CSE 341: Programming Languages

A Brief Introduction to Scheme (I)

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

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

A Functional Evaluation Model

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS 6110 S14 Lecture 1 Introduction 24 January 2014

A Func'onal Introduc'on. COS 326 David Walker Princeton University

LING 581: Advanced Computa7onal Linguis7cs. Lecture Notes April 16th

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS 360 Programming Languages Interpreters

Comp 311: Sample Midterm Examination

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

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

Properties and Definitions

Project 2: Scheme Interpreter

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

CS558 Programming Languages. Winter 2013 Lecture 3

Environments

Scheme as implemented by Racket

CS115 - Module 9 - filter, map, and friends

Thinking Induc,vely. COS 326 David Walker Princeton University

Welcome to CS 115 (Winter 2018)

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Fall 2017, Lyn Turbak

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

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

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Lecture #24: Programming Languages and Programs

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Functional Programming Languages (FPL)

61A Lecture 2. Wednesday, September 4, 2013

Document Databases: MongoDB

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

Hofl: First-class Functions and Scoping

CS 314 Principles of Programming Languages

Transcription:

Func%ons in Racket Racket Functions: most important building block in Racket (and 25) Functions/procedures/methods/subroutines abstract over computations Like Java methods, Python functions have arguments and result But no classes, this, return, etc. Examples: (define dbl (lambda (x) (* x 2))) CS25 Programming Languages Fall 207, Lyn Turbak (define quad (lambda (x) (dbl (dbl x)))) (define avg (lambda (a b) (/ (+ a b) 2))) (define sqr (lambda (n) (* n n))) Department of Computer Science Wellesley College (define n 0) (define small? (lambda (num) (<= num n))) 2 lambda denotes a anonymous func+on Syntax: (lambda (Id... Idn) Ebody) lambda: keyword that introduces an anonymous func+on (the func+on itself has no name, but you re welcome to name it using define) Id... Idn: any iden+fiers, known as the parameters of the func+on. Ebody: any expression, known as the body of the func+on. It typically (but not always) uses the func+on parameters. Evalua+on rule: A lambda expression is just a value (like a number or boolean), so a lambda expression evaluates to itself! What about the func+on body expression? That s not evaluated un+l later, when the func+on is called. (Synonyms for called are applied and invoked.) 3 Func+on applica+ons (calls, invoca+ons) To use a func+on, you apply it to arguments (call it on arguments). E.g. in Racket: (dbl 3), (avg 8 2), (small? 7) Syntax: (E0 E En) A func+on applica+on expression has no keyword. It is the only parenthesized expression that doesn t begin with a keyword. E0: any expression, known as the rator of the func+on call (i.e., the func+on posi+on). E En: any expressions, known as the rands of the call (i.e., the argument posi+ons). Evalua+on rule:. Evaluate E0 En in the current environment to values V0 Vn. 2. If V0 is not a lambda expression, raise an error. 3. If V0 is a lambda expression, returned the result of applying it to the argument values V Vn (see following slides). 4

Func+on applica+on What does it mean to apply a func+on value (lambda expression) to argument values? E.g. ((lambda (x) (* x 2)) 3) Func+on applica+on: subs+tu+on model Example : ((lambda (x) (* x 2)) 3) Subs+tute 3 for x in (* x 2) (* 3 2) We will explain func+on applica+on using two models:. The subs%tu%on model: subs+tute the argument values for the parameter names in the func+on body. Later This lecture 2. The environment model: extend the environment of the func+on with bindings of the parameter names to the argument values. 5 Example 2: Now evaluate (* 3 2)to 6 (/ (+ 8 2) 2) Subs+tute 8 for a and 2 for b in(/ (+ a b) 2) Now evaluate (/ (+ 8 2) 2) to 0 6 Subs+tu+on nota+on We will use the nota+on E[V,, Vn/Id,, Idn] to indicate the expression that results from subs+tu+ng the values V,, Vn for the iden+fiers Id,, Idn in the expression E. For example: (* x 2)[3/x] stands for (* 3 2) (/ (+ a b) 2)[8,2/a,b] stands for (/ (+ 8 2) 2) (if (< x z) (+ (* x x) (* y y)) (/ x y)) [3,4/x,y] stands for(if (< 3 z) (+ (* 3 3) (* 4 4)) (/ 3 4)) Avoid this common subs+tu+on bug Students some+mes incorrectly subs+tute the argument values into the parameter posi+ons: Makes no sense (lambda (8 2) (/ (+ 8 2) 2)) When subs+tu+ng argument values for parameters, only the modified body should remain. The lambda and params disappear! It turns out that there are some very tricky aspects to doing subs+tu+on correctly. We ll talk about these when we encounter them. 7 (/ (+ 8 2) 2) 8

Small-step func+on applica+on rule: subs+tu+on model ( (lambda (Id Idn) Ebody) V Vn ) Ebody[V Vn/Id Idn] [func+on call (a.k.a.apply)] Note: could extend this with no+on of current environment Small-step seman+cs: func+on example (quad 3) ((lambda (x) (dbl (dbl x))) 3) [varref] (dbl (dbl 3)) [func+on call] ((lambda (x) (* x 2)) (dbl 3)) [varref] ((lambda (x) (* x 2)) ((lambda (x) (* x 2)) 3)) [varref] ((lambda (x) (* x 2)) (* 3 2)) [func+on call] ((lambda (x) (* x 2)) 6) [mul+plica+on] (* 6 2) [func+on call] 2 [mul+plica+on] 9 0 Small-step substitution model semantics: your turn Suppose env3 = n 0, small? (lambda (num) (<= num n)), sqr (lambda (n) (* n n)) Give an evalua+on deriva+on for (small? (sqr n))# env3 Stepping back: name issues Do the par+cular choices of func+on parameter names macer? Is there any confusion caused by the fact that dbl and quad both use x as a parameter? Are there any parameter names that we can t change x to in quad? In (small? (sqr n)), is there any confusion between the global parameter name n and parameter n in sqr? Is there any parameter name we can t use instead of num in small? 2

Evalua+on Contexts Although we will not do so here, it is possible to formalize exactly how to find the next redex in an expression using so-called evalua%on contexts. For example, in Racket, we never try to reduce an expression within the body of a lambda. ( (lambda (x) (+ (* 4 5) x)) (+ 2) ) not this this is the first redex We ll see later in the course that other choices are possible (and sensible). 3 Big step func+on call rule: subs+tu+on model E0 # env (lambda (Id Idn) Ebody) E # env V En # env Vn Ebody[V Vn/Id Idn] # env Vbody (E0 E En) # env Vbody Note: no need for func+on applica+on frames like those you ve seen in Python, Java, C, (func+on call) 4 Subs+tu+on model deriva+on Suppose env2 = dbl (lambda (x) (* x 2)), quad (lambda (x) (dbl (dbl x))) quad # env2 (lambda (x) (dbl (dbl x))) 3 # env2 3 dbl # env2 (lambda (x) (* x 2)) dbl # env2 (lambda (x) (* x 2)) 3 # env2 3 (* 3 2) # env2 6 [mul+plica+on rule, subparts omiced] [func+on call (dbl 3)# env2 6 (* 6 2) # env2 2 (mul+plica+on rule, subparts omiced) (func+on call) (dbl (dbl 3))# env2 2 (func+on call) (quad 3)# env2 2 5 Recursion Recursion works as expected in Racket using the subs+tu+on model (both in big-step and small-step seman+cs). There is no need for any special rules involving recursion! The exis+ng rules for defini+ons, func+ons, and condi+onals explain everything. (define fact (lambda (n) (if (= n 0) (* n (fact (- n )))))) What is the value of (fact 3)? 6

Small-step recursion derivation for (fact 4) [] Let s use the abbrevia+on λ_fact for the expression (λ (n) (if (= n 0) (* n (fact (- n ))))) ({fact} 4) {(λ_fact 4)} (if {(= 4 0)} (* 4 (fact (- 4 )))) {(if #f (* 4 (fact (- 4 ))))} (* 4 ({fact} (- 4 ))) (* 4 (λ_fact {(- 4 )})) (* 4 {(λ_fact 3)}) (* 4 (if {(= 3 0)} (* 3 (fact (- 3 ))))) (* 4 {(if #f (* 3 (fact (- 3 ))))}) (* 4 (* 3 ({fact} (- 3 )))) (* 4 (* 3 (λ_fact {(- 3 )}))) (* 4 (* 3 {(λ_fact 2)})) (* 4 (* 3 (if {(= 2 0)} (* 2 (fact (- 2 )))))) (* 4 (* 3 {(if #f (* 2 (fact (- 2 ))))})) continued on next slide 7 Small-step recursion derivation for (fact 4) [2] continued from previous slide (* 4 (* 3 (* 2 ({fact} (- 2 ))))) (* 4 (* 3 (* 2 (λ_fact {(- 2 )})))) (* 4 (* 3 (* 2 {(λ_fact )}))) (* 4 (* 3 (* 2 {(λ_fact )}))) (* 4 (* 3 (* 2 (if {(= 0)} (* (fact (- ))))))) (* 4 (* 3 (* 2 {(if #f (* (fact (- ))))}))) (* 4 (* 3 (* 2 (* ({fact} (- )))))) (* 4 (* 3 (* 2 (* (λ_fact {(- )}))))) (* 4 (* 3 (* 2 (* {(λ_fact 0)})))) (* 4 (* 3 (* 2 (* (if {(= 0 0)} (* 0 (fact (- 0 )))))))) (* 4 (* 3 (* 2 (* {(if #t (* 0 (fact (- 0 ))))})))) (* 4 (* 3 (* 2 {(* )}))) (* 4 (* 3 {(* 2 )})) (* 4 {(* 3 2)}) {(* 4 6)} 24 8 Abbreviating derivations with * E * E2 means E reduces to E2 in zero or more steps ({fact} 4) {(λ_fact 4)} * (* 4 {(λ_fact 3)}) * (* 4 (* 3 {(λ_fact 2)})) * (* 4 (* 3 (* 2 {(λ_fact )}))) * (* 4 (* 3 (* 2 (* {(λ_fact 0)})))) * (* 4 (* 3 (* 2 {(* )}))) (* 4 (* 3 {(* 2 )})) (* 4 {(* 3 2)}) {(* 4 6)} 24 Recursion: your turn Show an abbreviated small-step evalua+on of (pow 5 3) where pow is defined as: (define pow (lambda (base exp) (if (= exp 0) (* base (pow base (- exp )))))) How many mul+plica+ons are performed in (pow 2 0)? (pow 2 00)? (pow 2 000)? What is the stack depth (# pending mul+plies) in these cases? 9 20

Recursion: your turn 2 Show an abbreviated small-step evalua+on of (fast-pow 2 0) with the following defini+ons : (define square (lambda (n) (* n n))) (define even? (lambda (n) (= 0 (remainder n 2)))) (define fast-pow (lambda (base exp) (if (= exp 0) (if (even? exp) (fast-pow (square base ) (/ exp 2)) (* base (fast-pow base (- exp ))))))) How many mul+plica+ons are performed in (pow 2 0)? (pow 2 00)? (pow 2 000)? What is the stack depth (# pending mul+plies) in these cases? 2 Tree Recursion: fibonacci Suppose the global env contains binding fib λ_fib, where λ_fib abbreviates (λ (n) (if (<= n ) n (+ (fib (- n )) (fib (- n 2))))) ({fib} 4) {(λ_fib 4)} * (+ {(λ_fib 3)} (fib (- 4 2))) * (+ (+ {(λ_fib 2)} (fib (- 3 2))) (fib (- 4 2))) * (+ (+ (+ {(λ_fib )} (fib (- 2 2))) (fib (- 3 2))) (fib (- 4 2))) * (+ (+ (+ {(λ_fib 0)}) (fib (- 3 2))) (fib (- 4 2))) * (+ (+ {(+ 0)} (fib (- 3 2))) (fib (- 4 2))) * (+ (+ {(λ_fib )}) (fib (- 4 2))) * (+ {(+ )} (fib (- 4 2))) * (+ 2 {(λ_fib 2)}) * (+ 2 (+ {(λ_fib )} (fib (- 2 2)))) * (+ 2 (+ {(λ_fib 0)})) * (+ 2 {(+ 0)}) {(+ 2 )} 3 22 Syntac+c sugar: func+on defini+ons Syntactic sugar: simpler syntax for common pattern. Implemented via textual translation to existing features. i.e., not a new feature. Example: Alternative function definition syntax in Racket: (define (Id_funName Id Idn) E_body) desugars to (define Id_funName (lambda (Id Idn) E_body)) (define (dbl x) (* x 2)) (define (quad x) (dbl (dbl x))) (define (pow base exp) (if (< exp ) (* base (pow base (- exp ))))) syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax 23 Racket Operators are Actually! Surprise! In Racket, opera+ons like (+ e e2), (< e e2)and (not e)are really just func+on calls! There is an ini+al top-level environment that contains bindings for built-in func+ons like: + addition function, - subtraction function, * multiplication function, < less-than function, not boolean negation function, (where some built-in functions can do special primitive things that regular users normally can t do --- e.g. add two numbers) 24

Summary So Far Racket declarations: defini+ons: (define Id E) Racket expressions: condi+onals: (if Etest Ethen Eelse) func+on values: (lambda (Id Idn) Ebody) Func+on calls: (Erator Erand Erandn) Note: arithmetic and relation operations are just function calls What about? Assignment? Don t need it! Loops? Don t need them! Use tail recursion, coming soon. Data structures? Glue together two values with cons (next time) 25