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

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

LISP: LISt Processing

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: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

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

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

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

Introduc)on To Standard ML

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

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

Ways to implement a language

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

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

Recursive List Func.ons in Racket

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

Related Course Objec6ves

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

Functional abstraction

FOFL and FOBS: First-Order Functions

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

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

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

Functional Programming. Pure Functional Programming

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

Chapter 1. Fundamentals of Higher Order Programming

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

Implemen'ng OCaml in OCaml

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

CS 360 Programming Languages Interpreters

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CSCC24 Functional Programming Scheme Part 2

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

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

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

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

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

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

SCHEME AND CALCULATOR 5b

Local defini1ons. Func1on mul1ples- of

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

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

Interpre'ng and Compiling Intex

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

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

Func%onal Programming in Scheme and Lisp

CS1 Recitation. Week 1

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

To figure this out we need a more precise understanding of how ML works

Func%onal Programming in Scheme and Lisp

Module 8: Local and functional abstraction

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Module 3: New types of data

CS2500 Exam 2 Fall 2011

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

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

Principles of Programming Languages

Algorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science

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

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Concepts of programming languages

A Simple Syntax-Directed Translator

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Programming Paradigms

Interpre'ng and Compiling Intex SOLUTIONS

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

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

Functional Programming Languages (FPL)

2010Fa CS10 Online Final Answers Section 1

The Environment Model. Nate Foster Spring 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.

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Functional Programming. Pure Functional Languages

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

The Substitution Model. Nate Foster Spring 2018

CS558 Programming Languages. Winter 2013 Lecture 3

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

D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Scheme as implemented by Racket

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

Bits, Bytes, and Integers

Thinking Induc,vely. COS 326 David Walker Princeton University

A Brief Introduction to Scheme (I)

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Hofl: First-class Functions and Scoping

CS1 Lecture 22 Mar. 6, 2019

Functions. Nate Foster Spring 2018

The syntax and semantics of Beginning Student

Transcription:

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

Racket Func+ons Functions: most important building block in Racket (and 251) 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))) (define quad (lambda (x) (dbl (dbl x)))) (define avg (lambda (a b) (/ (+ a b) 2))) (define sqr (lambda (n) (* n n))) (define n 10) (define small? (lambda (num) (<= num n))) 4-2

lambda denotes a anonymous func+on Syntax: (lambda (id1... idn) e) lambda: keyword that introduces an anonymous func+on (the func+on itself has no name, but you re welcome to name it using define) id1... idn: any iden+fiers, known as the parameters of the func+on. e: 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. 4-3

Func+on calls (applica+ons) To use a func+on, you call it on arguments (apply it to arguments). E.g. in Racket: (dbl 3), (avg 8 12), (small? 17) Syntax: (e0 e1 en) A func+on call expression has no keyword. A func+on call because it s 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). e1 en: any expressions, known as the rands of the func+on call (i.e., the argument posi+ons). Evalua+on rule: 1. 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 v1 vn (see following slides). 4-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) ((lambda (a b) (/ (+ a b) 2) 8 12) We will explain func+on applica+on using two models: 1. The subs2tu2on model: subs+tute the argument values for the parameter names in the func+on body. 2. The environment model: extend the environment of the func+on with bindings of the parameter names to the argument values. 4-5

Func+on applica+on: subs+tu+on model Example 1: ((lambda (x) (* x 2)) 3) Subs+tute 3 for x in (* x 2) and evaluate the result: (* 3 2) 6 (environment doesn t maqer in this case) Example 2: ((lambda (a b) (/ (+ a b) 2) 8 12) Subs+tute 3 for x in (* x 2) and evaluate the result: (/ (+ 8 12) 2) 10 (environment doesn t maqer in this case) 4-6

Subs+tu+on nota+on We will use the nota+on e[v1,, vn/id1,, idn] to indicate the expression that results from subs+tu+ng the values v1,, vn for the iden+fiers id1,, idn in the expression e. For example: (* x 2)[3/x] stands for (* 3 2) (/ (+ a b) 2)[8,12/a,b] stands for (/ (+ 8 12) 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)) 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. 4-7

Func+on call rule: subs+tu+on model e0 # env (lambda (id1 idn) e_body) e1 # env v1 en # env vn e_body[v1 vn/id1 idn] # env v_body (e0 e1 en) # env v_body (func+on call) Note: no need for func+on applica+on frames like those you ve seen in Python, Java, C, 4-8

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 omiqed) (func+on call) (dbl 3)# env2 6 (* 6 2) # env2 12 (mul+plica+on rule, subparts omiqed) (func+on call) (dbl (dbl 3))# env2 12 (func+on call) (quad 3)# env2 12 4-9

Subs+tu+on model deriva+on: your turn Suppose env3 = n 10, small? (lambda (num) (<= num n)) sqr (lambda (n) (* n n)) Give an evalua+on deriva+on for (small? (sqr n))# env3 4-10

Stepping back: name issues Do the par+cular choices of func+on parameter names maqer? 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? 4-11

Small- step vs. big- step seman+cs The evalua+on deriva+ons we ve seen so far are called a big- step seman2cs because the deriva+on e # env2 v explains the evalua+on of e to v as one big step jus+fied by the evalua+on of its subexpressions. An alterna+ve way to express evalua+on is a small- step seman2cs in which an expression is simplified to a value in a sequence of steps that simplifies subexpressions. You do this all the +me when simplifying math expressions, and we can do it in Racket, too. E.g; (- (* (+ 2 3) 9) (/ 18 6)) (- (* 5 9) (/ 18 6)) (- 45 (/ 18 6)) (- 45 3) 42 4-12

Small- step seman+cs: intui+on Scan lec to right to find the first redex (nonvalue subexpression that can be reduced to a value) and reduce it: (- (* (+ 2 3) 9) (/ 18 6)) (- (* 5 9) (/ 18 6)) (- 45 (/ 18 6)) (- 45 3) 42 4-13

Small- step seman+cs: reduc+on rules There are a small number of reduc+on rules for Racket. These specify the redexes of the language and how to reduce them. The rules ocen require certain subparts of a redex to be values in order to be applicable. id v, where id v in the current environment* (varref) (+ v1 v2 ) v, where v is the sum of v1 and v2 (addi+on) There are similar rules for other arithme+c operators (if #t e_then e_else ) e_then (if true) (if #f e_then e_else ) e_false (if false) ((lambda (id1 idn) e_body) v1 vn ) e_body[v1 vn/id1 idn] (func+on call) * In a more formal approach, the nota+on would make the environment explicit. E.g., e # env v 4-14

Small- step seman+cs: condi+onal example (+ (if (< 1 2) (* 3 4) (/ 5 6)) 7) (+ (if #t (* 3 4) (/ 5 6)) 7) (+ (* 3 4) 7) (+ 12 7) 19 4-15

Small- step seman+cs: errors as stuck expressions Similar to big- step seman+cs, we model errors (dynamic type errors, divide by zero, etc.) in small- step seman+cs as expressions in which the evalua+on process is stuck because no reduc+on rule is matched. For example (- (* (+ 2 3) #t) (/ 18 6)) (- (* 5 #t) (/ 18 6)) (if (= 2 (/ (+ 3 4) (- 5 5))) 8 9) (if (= 2 (/ 7 (- 5 5))) 8 9) (if (= 2 (/ 7 0)) 8 9) 4-16

Small- step seman+cs: func+on example (quad 3) ((lambda (x) (dbl (dbl x))) 3) (dbl (dbl 3)) ((lambda (x) (* x 2)) (dbl 3)) ((lambda (x) (* x 2)) ((lambda (x) (* x 2)) 3)) ((lambda (x) (* x 2))(* 3 2)) ((lambda (x) (* x 2)) 6) (* 6 2) 12 4-17

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 evalua2on contexts. For example, in Racket, we never try to reduce an expression within the body of a lambda. ((lambda (x) (+ (* 4 5) x)) (+ 1 2)) not this this is the first redex We ll see later in the course that other choices are possible (and sensible). 4-18

Small-step semantics: your turn Use small- step seman+cs to evaluate (small? (sqr n)) Assume this is evaluated with respect to the same global environment used earlier. 4-19

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 pow (lambda (base exp) (if (= exp 0) 1 (* base (pow base (- exp 1)))))) What is the value of (pow 5 2)? 4-20

Recursion: your turn Define and test the following recursive func+ons in Racket: (fact n): Return the factorial of the nonnega+ve integer n (fib n): Return the nth Fibonacci number (sum-between lo hi): return the sum of the integers between integers lo and hi (inclusive) 4-21

Syntac+c sugar: func+on defini+ons syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax 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 id1 idn) e_body) desugars to (define id_funname (lambda (id1 idn) e_body)) (define (dbl x) (* x 2)) (define (quad x) (dbl (dbl x))) (define (pow base exp) (if (< exp 1) 1 (* base (pow base (- exp 1))))) 4-22

Racket Operators are Actually Func+ons! Surprise! In Racket, opera+ons like (+ e1 e2), (< e1 e2)are, and (not e)are really just func+on applica+ons! There is an ini+al top- level environment that contains bindings like: + addition function, - subtraction function, * multiplication function, < less-than function, not boolean negation function, 4-23

Summary So Far Racket declarations: defini+ons: (define id e) Racket expressions: condi+onals: (if e_test e_then e_else) func+on values: (lambda (id1 idn) e_body) Func+on calls: (e_rator e_rand1 e_randn) 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) 4-24