CSci 4223 Principles of Programming Languages

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

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

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

CSE 341 : Programming Languages

CSE341: Programming Languages Winter 2018 Unit 3 Summary Zach Tatlock, University of Washington

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

CSE 341 Section 5. Winter 2018

FUNCTIONAL PROGRAMMING

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

Higher-Order Functions

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

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Lecture 4: Higher Order Functions

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

Higher Order Functions in Haskell

An introduction introduction to functional functional programming programming using usin Haskell

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell Overview II (2A) Young Won Lim 8/23/16

List Functions, and Higher-Order Functions

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Lists. Michael P. Fourman. February 2, 2010

Chapter 3 Linear Structures: Lists

Haskell Overview II (2A) Young Won Lim 9/26/16

F28PL1 Programming Languages. Lecture 14: Standard ML 4

UNIVERSITETET I OSLO

Chapter 3 Linear Structures: Lists

CS 457/557: Functional Languages

Standard ML. Curried Functions. ML Curried Functions.1

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

Part I: Written Problems

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSci 4223 Principles of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

The Haskell HOP: Higher-order Programming

Lecture 21: Functional Programming in Python. List Comprehensions

List Processing in SML

CSCI-GA Scripting Languages

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

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Lecture 19: Functions, Types and Data Structures in Haskell

List Processing in SML

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

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

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

ML Built-in Functions

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSCE 314 Programming Languages

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Shell CSCE 314 TAMU. Higher Order Functions

A list is a finite sequence of elements. Elements may appear more than once

Functional Programming - 2. Higher Order Functions

A general introduction to Functional Programming using Haskell

Exercises on ML. Programming Languages. Chanseok Oh

CSCI-GA Final Exam

CS 440: Programming Languages and Translators, Spring 2019 Mon

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Recap: Functions as first-class values

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

A quick introduction to SML

301AA - Advanced Programming

Inductive Data Types

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

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

Functional Paradigm II

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

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

The type checker will complain that the two branches have different types, one is string and the other is int

Datatype declarations

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

CS 321 Programming Languages

CSCC24 Functional Programming Scheme Part 2

Standard ML. Data types. ML Datatypes.1

HIGHER-ORDER FUNCTIONS

INTRODUCTION TO HASKELL

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Chapter 1. Fundamentals of Higher Order Programming

Introduction to OCaml

CSE 341 Sample Midterm #2

CSE 3302 Programming Languages Lecture 8: Functional Programming

Topic 7: Algebraic Data Types

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Currying fun f x y = expression;

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

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

SNU Programming Language Theory

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

02157 Functional Programming Tagged values and Higher-order list functions

How does ML deal with +?

A Brief Introduction to Standard ML

Transcription:

CSci 4223 Principles of Programming Languages Lecture 11 Review Features learned: functions, tuples, lists, let expressions, options, records, datatypes, case expressions, type synonyms, pattern matching, exceptions, type variables, higher-order and anonymous functions, infix operators, type constructors Today: Function composition fold Currying (mmmm) Prof. Clarkson Spring 2013 2 Combine functions In math, f g(x) = f(g(x)) fun compose (f,g) = fn x => f (g x) Type ('b -> 'c) * ('a -> 'b) -> ('a -> 'c) but the REPL prints something equivalent FUNCTION COMPOSITION ML standard library provides this as infix operator o at s small-case letter O, as in Example (third version best): fun sqrt_of_abs i = Math.sqrt(Real.fromInt(abs i)) fun sqrt_of_abs i = (Math.sqrt o Real.fromInt o abs) i val sqrt_of_abs = Math.sqrt o Real.fromInt o abs 3 4 Left-to-right vs. right-to-left val sqrt_of_abs = Math.sqrt o Real.fromInt o abs In math, function composition is read right-to-left take absolute value; convert to real; take square root But programmers often prefer reading left-to-right Pipelines of functions are common in functional programming Pipeline operator is very popular (and predefined) in F# Can define ourselves in ML infix > fun x > f = f x fun sqrt_of_abs i = i > abs > Real.fromInt > Math.sqrt FOLD 5 6 1

Map Map fun map (f,xs) = x::xs => (f x)::(map(f,xs )) fun map (f,xs) = x::xs => (f x)::(map(f,xs )) map : ('a -> 'b) * 'a list -> 'b list map : ('a -> 'b) * 'a list -> 'b list map (fn x => shirt_color(x), [ ] bad style! = [gold, blue, red] map (shirt_color, [ ] = [gold, blue, red] 7 8 Filter fun filter (f,xs) = x::xs => if f x then x::(filter(f,rest)) else filter(f,rest) filter : ('a -> bool) * 'a list -> 'a list filter(is_vulcan, [ ] Another famous function: Fold foldl used to iterate over recursive data structures several synonyms/cousins: reduce, inject, etc. Accumulates an answer by repeatedly applying f to answer so far foldl(f,acc,[x1,x2,x3]) computes f(x3,f(x2,f(x1,acc))) fun foldl (f,acc,xs) = x::xs => foldl(f, f(x,acc), xs ) is version folds from the left = [ ] (er, half vulcan) 9 val foldl = fn : ('a * 'b -> 'b) * 'b * 'a list -> 'b 10 Foldl example val l = [ ] foldl(max_rank, Ensign, l) (* = Captain *) Another famous function: Fold foldr Folds from the right foldr(f,acc,[x1,x2,x3]) computes f(x1,f(x2,f(x3,acc))) fun foldr (f,acc,xs) = x::xs => f(x,foldr(f,acc,xs )) val foldr = fn : ('a * 'b -> 'b) * 'b * 'a list -> 'b 11 12 2

2/27/13 Examples with fold Examples with fold Implement so many other functions with fold! ese are useful and do not use private data fun rev xs = foldl(op ::, [], xs) fun length xs = foldl(fn (_,a) => a+1, 0, xs) fun map(f,xs) = foldr(fn (x,a) => (f x)::a, [], xs) fun filter(f,xs) = foldr(fn (x,a) => if f x then x::a else a, [], xs) fun f1 xs = fold((fn (x,y) => x+y), 0, xs) fun f2 xs = fold((fn (x,y) => x andalso y>=0), true, xs) These are useful and do use private data fun f3 (xs,hi,lo) = fold(fn (x,y) => x + (if y >= lo andalso y <= hi then 1 else 0)), 0, xs) fun f4 (g,xs) = fold(fn (x,y) => x andalso g y), true, xs) 13 14 e benefits of iterators ese iterator-like functions are not built-in to the language Just a programming pattern ough many languages have built-in support, which often allows stopping early without using exceptions is pattern separates recursive traversal from data processing Can reuse same traversal for different data processing Can reuse same data processing for different data structures CURRYING 15 Currying and Partial Application Recall every ML function takes exactly one argument Previously encoded n arguments via one n-tuple Another way: Take one argument and return a function that takes another argument and Called currying after famous logician Haskell Curry 16 Haskell B. Curry Curry-Howard isomorphism Types are logical formulas Programs are logical proofs fn x => x : a -> a 1900-1982 vs. 17 18 3

Currying and Partial Application Recall every ML function takes exactly one argument Example, with full and partial application: val sorted3 = fn x => fn y => fn z => z >= y andalso y >= x val true_ans = ((sorted3 7) 9) 11 val is_non_negative = (sorted3 0) 0 Syntactic sugar Currying is much prettier than we have indicated so far Can write e1 e2 e3 e4 in place of ((e1 e2) e3) e4 Can write fun f x y z = e in place of fun f x = fn y => fn z => e fun sorted3 x y z = z >= y andalso y >= x val true_ans = sorted3 7 9 11 val is_non_negative = sorted3 0 0 Result is a little shorter and prettier than the tupled version: fun sorted3 (x,y,z) = z >= y andalso y >= x val true_ans = sorted3(7,9,11) fun is_non_negative x = sorted3(0,0,x) 19 20 Return to the fold J e library s way In addition to being sufficient multi-argument functions and pretty, currying is useful because partial application is convenient Example: Often use higher-order functions to create other functions fun fold f acc xs = x::xs => fold f (f(acc,x)) xs fun sum xs = fold (fn (x,y) => x+y) 0 xs val sum_best = fold (op+) 0 So the SML standard library is fond of currying iterators See types for List.map, List.filter, List.foldl, etc. So calling them as though arguments are tupled won t work Another example is List.exists: fun exists predicate xs = [] => false x::xs => predicate xs orelse exists predicate xs val no = exists (fn x => x=7) [4,11,23] val has_seven = exists (fn x => x=7) 21 22 Another example Currying and partial application can be convenient even without higherorder functions fun zip xs ys = case (xs,ys) of ([],[]) => [] (x::xs,y::ys ) => (x,y)::(zip xs ys ) _ => raise Empty fun range i j = if i>j then [] else i :: range (i+1) j val countup = range 1 (* partial application *) fun add_number xs = zip (countup (length xs)) xs More combining functions What if you want to curry a tupled function or vice-versa? What if a function s arguments are in the wrong order for the partial application you want? Naturally, it s easy to write higher-order wrapper functions And their types are neat logical formulas fun other_curry1 f = fn x => fn y => f y x fun other_curry2 f x y = f y x fun curry f x y = f (x,y) fun uncurry f (x,y) = f x y 23 24 4

e Value Restriction Appears L If you use partial application to create a polymorphic function, it may not work due to the value restriction Warning about type vars not generalized And won t let you call the function is should surprise you; you did nothing wrong J but you still must change your code See the written lecture summary about how to work around this wart (and ignore the issue until it arises) e wart is there for good reasons, related to mutation and not breaking the type system 25 5