CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Similar documents
CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

FUNCTIONAL PROGRAMMING

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CSci 4223 Principles of Programming Languages

Introduction to OCaml

CS 321 Programming Languages

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

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

Functions. Nate Foster Spring 2018

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

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

Programming Languages and Compilers (CS 421)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSCI-GA Scripting Languages

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

CS3110 Spring 2017 Lecture 7 Specifications using types continued

CSCI 2041: First Class Functions

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Lecture 4: Higher Order Functions

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

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Gain familiarity and practice with folding and mapping. The following supplementary materials may be helpful in completing this assignment:

Programming Languages and Techniques (CIS120)

CS3110 Spring 2016 Lecture 6: Modules

CS 440: Programming Languages and Translators, Spring 2019 Mon

SNU Programming Language Theory

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

School of Computer Science

Introduction to OCaml

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

Functions Over Lists. Programming Languages and Compilers (CS 421) Structural Recursion. Functions Over Lists. Structural Recursion : List Example

The Substitution Model

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

List Functions, and Higher-Order Functions

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

The Environment Model

Programming Languages and Techniques (CIS120)

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

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

CSci 4223 Principles of Programming Languages

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

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

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

INF121: Functional Algorithmic and Programming

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

An introduction introduction to functional functional programming programming using usin Haskell

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE 505: Concepts of Programming Languages

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 421, Fall Sample Final Questions & Solutions

CMSC330. Objects, Functional Programming, and lambda calculus

Programming Languages and Compilers (CS 421)

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

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

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

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

Part I: Written Problems

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

The Substitution Model. Nate Foster Spring 2018

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE 341: Programming Languages

CSCC24 Functional Programming Scheme Part 2

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Graphical Untyped Lambda Calculus Interactive Interpreter

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

Exercise 1 ( = 24 points)

CSE 341 : Programming Languages

CSE 130 [Winter 2014] Programming Languages

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

Exercise 1 ( = 22 points)

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE341, Fall 2011, Midterm Examination October 31, 2011

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

CSE 341 : Programming Languages Midterm, Spring 2015

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Theorem Proving Principles, Techniques, Applications Recursion

Programming Languages Fall 2014

Lecture 5: The Untyped λ-calculus

Introduction to Scheme

Functional Programming. Lecture 2: Algebra

CMSC 330: Organization of Programming Languages. Lambda Calculus

Some Advanced ML Features

Transcription:

CS 3110 Lecture 6: Map and Fold Prof. Clarkson Spring 2015 Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Review Course so far: Syntax and semantics of (most of) OCaml Today: No new language features New idioms: Map, fold, and other higher-order functions

Question #1 How much of PS1 have you finished? A. None B. About 25% C. About 50% D. About 75% E. I m done!!!

Review: higher-order functions Functions are values Can use them anywhere we use values Arguments, results, parts of tuples, bound to variables, carried by datatype constructors or exceptions, First-class citizens of language, afforded all the rights of any other values Functions can take functions as arguments Functions can return functions as results functions can be higher-order 4

Review: anonymous functions (aka function expressions) Syntax: fun x -> e really fun p -> e Type checking: Conclude that fun x -> e : t1 -> t2 if e:t2 under assumption x:t1 Evaluation: A function is already a value

Lambda In PL, anonymous functions a.k.a. lambda expressions λx. e The lambda means what follows is an anonymous function x is its argument e is its body Just like fun x -> e, but slightly different syntax Standard feature of any functional language (ML, Haskell, Scheme, ) You ll see lambda show up in many places in PL, e.g.: PHP: http://www.php.net/manual/en/function.create-function.php A popular PL blog: http://lambda-the-ultimate.org/ Lambda style: https://www.youtube.com/watch?v=ci48kqp11f8 6

Review: currying Recall: every OCaml function takes exactly one argument Can encode n arguments with one n-tuple Or, can write function that takes one argument and returns a function that takes another argument and Called currying after famous logician Haskell vs. Curry 7

Haskell B. Curry 1900-1982 Languages Haskell and Curry named for him Curry-Howard isomorphism: Types are logical formulas Programs are logical proofs fun x -> x : 'a -> 'a 8

HUGE HIGHER-ORDER FUNCTIONS Discovery of the monolith: https://www.youtube.com/watch?v=ml1ozchixr0

Map bad style! map (fun x -> shirt_color(x)) [ ] = [gold, blue, red] 10

Map map shirt_color [ ] = [gold, blue, red] how would you implement map? 11

Map let rec map f xs = match xs with [] -> [] x::xs -> (f x)::(map f xs ) map : ('a -> 'b) -> 'a list -> 'b list Map is HUGE: You use it all the time once you know it Exists in standard library as List.map, but the idea can be used in any data structure (trees, stacks, queues ) 12

Question #2 What is value of lst after this code? let is_even x = (x mod 2 = 0) let lst = map is_even [1;2;3;4] A. [1;2;3;4] B. [2;4] C. [false; true; false; true] D. false E. None of the above

Question #2 What is value of lst after this code? let is_even x = (x mod 2 = 0) let lst = map is_even [1;2;3;4] A. [1;2;3;4] B. [2;4] C. [false; true; false; true] D. false E. None of the above

Filter filter is_vulcan [ ] = [ ] (er, half vulcan) how would you implement filter? 15

Filter let rec filter f xs = match xs with [] -> [] x::xs -> if f x then x::(filter f xs ) else filter f xs filter : ('a -> bool) -> 'a list -> 'a list Filter is also HUGE In library: List.filter 16

Question #3 What is value of lst after this code? let is_even x = (x mod 2 = 0) let lst = filter is_even [1;2;3;4] A. [1;2;3;4] B. [2;4] C. [false; true; false; true] D. false

Question #3 What is value of lst after this code? let is_even x = (x mod 2 = 0) let lst = filter is_even [1;2;3;4] A. [1;2;3;4] B. [2;4] C. [false; true; false; true] D. false

Iterators Map and filter are iterators Not built-in to the language, an idiom Benefit of iterators: separate recursive traversal from data processing Can reuse same traversal for different data processing Can reuse same data processing for different data structures leads to modular, maintainable, beautiful code! So far: iterators that change or omit data what about combining data? e.g., sum all elements of list 19

Fold v1.0 Idea: stick an operator between every element of list folding [1;2;3] with (+)! becomes 1+2+3! -->*! 6!

Fold v2.0 Idea: stick an operator between every element of list But list could have 1 element, so need an initial value folding [1] with 0 and (+)! becomes 0+1! -->*! 1!

Fold v2.0 Idea: stick an operator between every element of list But list could have 1 element, so need an initial value folding [1;2;3] with 0 and (+)! becomes 0+1+2+3! -->*! 6!

Fold v2.0 Idea: stick an operator between every element of list But list could have 1 element, so need an initial value Or list could be empty; just return initial value folding [] with 0 and (+)! becomes 0!

Question #4 What should the result of folding [1;2;3;4] with 1 and ( * ) be? A. 1 B. 24 C. 10 D. 0 E. None of the above

Question #4 What should the result of folding [1;2;3;4] with 1 and ( * ) be? A. 1 B. 24 C. 10 D. 0 E. None of the above

Fold v3.0 Idea: stick an operator between every element of list But list could have 1 element, so need an initial value Or list could be empty; just return initial value Implementation detail: iterate left-to-right or right-to-left? folding [1;2;3] with 0 and (+)! left to right becomes: ((0+1)+2)+3...fold_left! right to left becomes: 1+(2+(3+0))...fold_right! Both evaluate to 6; does it matter? Yes: not all operators are associative e.g. subtraction, division, exponentiation,

Question #5 Result of fold_right with input list [1;2;3], initial accumulator 1 and operator (-)? A. -5 B. -1 C. 1 D. Operator has the wrong type E. None of the above

Question #5 Result of fold_right with input list [1;2;3], initial accumulator 1 and operator (-)? A. -5 B. -1 C. 1 D. Operator has the wrong type E. None of the above

Question #6 Result of fold_left with input list [1;2;3], initial accumulator 1 and operator (-)? A. -5 B. -1 C. 1 D. Operator has the wrong type E. None of the above

Question #6 Result of fold_left with input list [1;2;3], initial accumulator 1 and operator (-)? A. -5 B. -1 C. 1 D. Operator has the wrong type E. None of the above

Fold v4.0 (+) accumulated a result of the same type as list itself What about operators that change the type? e.g., let cons x xs = x::xs cons : 'a -> 'a list -> 'a list! folding from the right [1;2;3] with [] and cons! should produce 1::(2::(3::[])) = [1;2;3]! So the operator needs to accept the accumulated result so far, and the next element of the input list which may have different types!

Fold for real Two versions in OCaml library: List.fold_left! : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a!!! List.fold_right! : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b!

Fold for real Two versions in OCaml library: List.fold_left! : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a!!! List.fold_right! : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b! Operator mnemonic: operator takes in accumulator on the (left right)

Fold for real Two versions in OCaml library: List.fold_left! : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a!!! List.fold_right! : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b! Input list

Fold for real Two versions in OCaml library: List.fold_left! : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a!!! List.fold_right! : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b! Initial value of accumulator mnemonic: fold takes in accumulator on the (left right) of the list

Fold for real Two versions in OCaml library: List.fold_left! : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a!!! List.fold_right! : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b! Final value of accumulator

fold_left! let rec fold_left f acc xs = match xs with [] -> acc x::xs -> fold_left f (f acc x) xs Accumulates an answer by repeatedly applying f to answer so far, starting with initial value acc,! folding from the left fold_left f acc [a;b;c]! computes f (f (f acc a) b) c! 37

fold_right! let rec fold_right f xs acc = match xs with [] -> acc x::xs -> f x (fold_right f xs acc) Accumulates an answer by repeatedly applying f to answer so far, starting with initial value acc,! folding from the right fold_right f [a;b;c] acc! computes f a (f b (f c acc))! 38

Behold the HUGE power of fold Implement so many other functions with fold! let rev xs = fold_left (fun xs x -> x::xs) [] xs let length xs = fold_left (fun a _ -> a+1) 0 xs let map f xs = fold_right (fun x a -> (f x)::a) xs [] let filter f xs = fold_right (fun x a -> if f x then x::a else a) xs [] 39

Beware the efficiency of fold Implementation of fold_left more space efficient than fold_right for long lists what is "long"? maybe 10,000 elements But that doesn t mean that one is strictly better than the other More in recitation

Map-Reduce Fold has many synonyms/cousins in various functional languages, including scan and reduce! Google organizes large-scale data-parallel computations with Map-Reduce open source implementation by Apache called Hadoop [Google s Map-Reduce] abstraction is inspired by the map and reduce primitives present in Lisp and many other functional languages. We realized that most of our computations involved applying a map operation to each logical record in our input in order to compute a set of intermediate key/value pairs, and then applying a reduce operation to all the values that shared the same key in order to combine the derived data appropriately." [Dean and Ghemawat, 2008]