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

Similar documents
FUNCTIONAL PROGRAMMING

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

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

Introduction to OCaml

CS 321 Programming Languages

CSci 4223 Principles of Programming Languages

Functions. Nate Foster Spring 2018

Programming Languages and Compilers (CS 421)

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

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

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

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

CSCI 2041: First Class Functions

# 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

School of Computer Science

CSE 130 [Winter 2014] Programming Languages

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

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

Programming Languages and Techniques (CIS120)

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

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

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

List Functions, and Higher-Order Functions

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

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

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

CS3110 Spring 2017 Lecture 7 Specifications using types continued

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

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

The Environment Model

SNU Programming Language Theory

The Haskell HOP: Higher-order Programming

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

CSCI-GA Scripting Languages

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

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

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Programming Languages and Techniques (CIS120)

CSCC24 Functional Programming Scheme Part 2

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

Programming Languages and Techniques (CIS120)

Functional Programming in Haskell for A level teachers

Programming Languages and Compilers (CS 421)

CSE 341 : Programming Languages Midterm, Spring 2015

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

CS 421, Fall Sample Final Questions & Solutions

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

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

CSE 341: Programming Languages

Async. Prof. Clarkson Fall Today s music: It's Gonna be Me by *NSYNC

The Substitution Model

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

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

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

Types, Type Inference and Unification

Some Advanced ML Features

Lecture 4: Higher Order Functions

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

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

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

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

Playing with Fun. Walter Cazzola. Playing with Fun currying partial evaluation map&reduce iteration var args. References.

CS 457/557: Functional Languages

CS 11 Ocaml track: lecture 2

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

CMSC330 Fall 2014 Midterm 2 Solutions

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

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341, Fall 2011, Midterm Examination October 31, 2011

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

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

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

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

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

CMSC 330, Fall 2013, Practice Problems 3

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

LECTURE 16. Functional Programming

Recap: Functions as first-class values

Functional Programming Languages (FPL)

Amortized Analysis. Prof. Clarkson Fall Today s music: : "Money, Money, Money" by ABBA

CS115 - Module 9 - filter, map, and friends

CS421 Spring 2014 Midterm 1

More Lambda Calculus and Intro to Type Systems

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

Transcription:

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

Question How much progress have you made on A1? A. I'm still figuring out how Enigma works. B. My code can cipher single letters. C. My code can cipher multiple letters, but stepping is still iffy. D. I'm done with cipher and simulate. E. I've finished the scavenger hunt, too.

Review Previously in 3110: Lists: OCaml's awesome built-in datatype Pattern matching: an awesome feature not found in most imperative languages Today: No new language features New idioms and library functions: Map, fold, and other higher-order functions

Review: higher-order functions Functions are values Can use them anywhere we use values Arguments, results, bound to variables... Functions can take functions as arguments Functions can return functions as results 4

Review: anonymous functions (aka function expressions) Syntax: fun x -> 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 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 Java 8: https://docs.oracle.com/javase/tutorial/java/javaoo/ lambdaexpressions.html A popular PL blog: http://lambda-the-ultimate.org/ Lambda style: https://www.youtube.com/watch?v=ci48kqp11f8 6

HUGE HIGHER-ORDER FUNCTIONS

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

Map map shirt_color [ ] = [gold; blue; red] 9

Map How to implement? Let's see some special cases... Write a function that adds 1 to every element of a list Write a function that concatenates "3110" to every element of a list

Map let rec add1 = function [] -> [] h::t -> (h+1)::(add1 t) let rec concat3110 = function [] -> [] h::t -> (h^"3110")::(concat3110 t)...notice the common structure

Map let rec add1 = function [] -> [] h::t -> (h+1)::(add1 t) let rec concat3110 = function [] -> [] h::t -> (h^"3110")::(concat3110 t) notice the common structure...same except for the blue part, which says what to do with head...which is what the function passed to map does

Map let rec map f = function [] -> [] 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 ) 13

Map let add1 = List.map (fun x -> x+1) let concat3110 = List.map (fun s -> s^"3110") Note the separation of concerns: List.map knows how to traverse the list The function passed in knows how to transform each element

Question 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

Question 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

Filter filter is_vulcan [ ] = [ ] (er, half vulcan) 17

Filter let rec filter f = function [] -> [] x::xs -> if f x then x::(filter f xs) else filter f xs filter : ('a -> bool) -> 'a list -> 'a list In library: List.filter (library implementation is tail recursive; the one above is not) 18

Question 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 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? 21

Combining elements Write a function that sums all the elements of a list Write a function that concatenates all the elements of a list

Combining elements let rec sum = function [] -> 0 h::t -> h + (sum t) let rec concat = function [] -> "" h::t -> h ^ (concat t) notice the common structure

Combining elements let rec sum = function [] -> 0 h::t -> h + (sum t) let rec concat = function [] -> "" h::t -> h ^ (concat t) notice the common structure...same except for the blue part, which gives a value to return for empty list a function to combine head with result of recursive call on tail

Combining elements let rec combine init op = function [] -> init h::t -> op h (combine init op t) let sum = combine 0 (+) let concat = combine "" (^) combining elements, using init and op, is the essential idea behind library functions known as fold

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

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

List.fold_right List.fold_right f [a;b;c] init computes f a (f b (f c init)) Accumulates an answer by repeatedly applying f to an element of list and answer so far folding in list elements from the right

List.fold_right let rec fold_right f xs acc = match xs with [] -> acc x::xs' -> f x (fold_right f xs' acc) Note: fold_right is the same as combine (just with argument order and names changed)

List.fold_left List.fold_left f init [a;b;c] computes f (f (f init a) b) c Accumulates an answer by repeatedly applying f to "answer so far" and an element of list folding in list elements from the left

List.fold_left let rec fold_left f acc xs = match xs with [] -> acc x::xs' -> fold_left f (f acc x) xs' Note: fold_left is a different computation than fold_right or combine...what are the differences?

Difference 1: Left vs. right folding [1;2;3] with 0 and (+) left to right: ((0+1)+2)+3 right to left: 1+(2+(3+0)) Both evaluate to 6; does it matter? Yes: not all operators are associative, e.g. subtraction, division, exponentiation,

Difference 2: Tail recursion Which of these is tail recursive? let rec fold_left f acc xs = match xs with [] -> acc x::xs' -> fold_left f (f acc x) xs' let rec fold_right f xs acc = match xs with [] -> acc x::xs' -> f x (fold_right f xs' acc) A. neither B. fold_left C. fold_right D. both fold_left and fold_right E. I don't know

Difference 2: Tail recursion Which of these is tail recursive? let rec fold_left f acc xs = match xs with [] -> acc x::xs' -> fold_left f (f acc x) xs' let rec fold_right f xs acc = match xs with [] -> acc x::xs' -> f x (fold_right f xs' acc) A. neither B. fold_left C. fold_right D. both fold_left and fold_right E. I don't know

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b Final value of accumulator

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b Initial value of accumulator

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b Input list

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b Operator

Difference 3: Types List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b Can't keep the argument order straight? Me neither. There is actually a rational design (accumulator is always to left/right of list (element)). The ListLabels module helps.

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 []

MapReduce Fold has many synonyms/cousins in various functional languages, including scan and reduce Google organizes large-scale data-parallel computations with MapReduce open source implementation by Apache called Hadoop [Google s MapReduce] 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]

Enrollment The fire-code capacity of Olin 155 is the limit At last count, there were 20-30 more people who wanted to take 3110 than there were seats available I cannot add students to the course myself The CS department does not do waitlists or prioritization for enrollment in 3110 But the department has agreed to open a limited number of seats above the fire-code capacity Keep checking Student Center today 3110 will be offered in the spring, and historically is always undersubscribed then

Upcoming events [today] Add deadline [Thursday] A1 soft deadline [Saturday] A1 hard deadline This is huge. THIS IS 3110