CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Exercise 4 Recursion. Functions – a short reminder a group of variables and statements that is assigned a name a sub-program  inside main we can call.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Chapter 9: Functional Programming in a Typed Language.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming
CSE 341 Lecture 5 efficiency issues; tail recursion; print
Another problem to solve…
Important Concepts from Clojure
Important Concepts from Clojure
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE 341 Lecture 3 let expressions; pattern matching Ullman
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
FP Foundations, Scheme In Text: Chapter 14.
slides created by Marty Stepp
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
Another problem to solve…
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 12 Equivalence
CSCE 314: Programming Languages Dr. Dylan Shell
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
Announcements Quiz 5 HW6 due October 23
CSE 341 Lecture 7 anonymous functions; composition of functions Ullman 5.1.3, 5.6 slides created by Marty Stepp
Important Concepts from Clojure
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE 341 Lecture 2 lists and tuples; more functions; mutable state
CSE341: Programming Languages Lecture 12 Equivalence
Another problem to solve…
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 12 Equivalence
Presentation transcript:

CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp

2 Exceptions (5.2) exception: An object representing an error.  can be generated ("raised") and repaired ("handled")  an elegant way to provide non-local error recovery exceptions can be used in ML for many reasons:  to bail out of a function whose preconditions are violated  "partial" functions that don't map entire domain to range  when a function doesn't know how to handle a particular problem (e.g., file not found; empty list; etc.) ...

3 Raising an exception (* defining an exception type *) exception name [of parameterTypes]; (* raising ("throwing") an exception *) raise exceptionName (parameterValues); ML includes many pre-defined exception types, but you can (and often should) define your own

4 Raising exception example exception Negative; (* Computes n!, or 1*2*3*...*n-1*n. *) fun factorial(0) = 1 | factorial(n) = if n < 0 then raise Negative else n * factorial(n - 1); - factorial(~4); uncaught exception Negative raised at: stdIn:

5 Exception with parameter exception Negative of string; (* Computes n!, or 1*2*3*...*n-1*n. *) fun factorial(0) = 1 | factorial(n) = if n < 0 then raise Negative("Can't pass a ¬ negative number for n!") else n * factorial(n - 1);

6 Handling an exception (5.2.3) expression1 handle exception => expression2 handling an exception stops it from going all the way up the call stack and stopping the program with an error the above code tries to compute expression1, but...  if that computation raises an exception of type exception, then expression1 will be replaced by expression2.  The exception => expression2 syntax is an example of a match, which we'll see more later.

7 Handling exception example (* Returns 2 * n!. A silly function. If factorial fails, produces 0. *) fun example(n) = 2 * factorial(n) handle Negative => 0; - example(4); val it = 48 : int - example(~3); val it = 0 : int

8 Operators as functions Every operator in ML is really a function defined with op : - op +; val it = fn : int * int -> int - op +(2, 5); val it = 7 : int - op *(op +(2, 5), op ~(4)); (* (2+5)*~4 *) val it = ~28 : int - op ^("hello", "world"); val it = "helloworld" : string

9 Defining an operator (* if defining a binary operator *) infix operator; fun op operator = expression; The operator can call itself recursively in its own expression as: op operator(parameters)

10 Defining operator example (* Exponentiation operator, computes x^y. Not tail-recursive. Fails for y<0. *) infix ^^; fun op ^^(x, 0) = 1 | op ^^(x, y) = x * op ^^(x, y - 1); - 2 ^^ 10; val it = 1024 : int Exercise: Define an operator -- such that a--b will create a list of the integers [a, a+1, a+2,..., b-1, b].

11 Defining operator solution (* x--y Produces [x,x+1,...,y-1,y]. Not tail-recursive. *) infix --; fun op --(x, y) = if x > y then [] else x :: op --(x+1, y); (* alternate version *) fun x--y = if x > y then [] else x :: (x+1)--y;

12 Functions as values in ML, a variable is just a symbol in the environment that maps from a name to a value a function is actually just a symbol as well; it maps from a function name to a piece of code to execute you can assign a variable ( val ) to refer to a function: - val xyz = factorial; val xyz = fn : int -> int - xyz(5); val it = 120 : int

13 Functions as parameters Since functions are values, we can pass them as parameters to other functions! - fun callAndAdd1(f) = f(4) + 1; val callAndAdd1 = fn : (int -> int) -> int - callAndAdd1(factorial); val it = 25 : int

14 Higher-order functions (5.4) higher-order function: A function that accepts another function as input and/or produces a function as output.  callAndAdd1 is higher-order, as is apply below. - fun apply(f, x) = f(x); val apply = fn : ('a -> 'b) * 'a -> 'b - apply(round, 3.54); val it = 4 : int

15 Common higher-order functions Many functional languages provide the following functions: map(F, list) : Applies F to each element of the list [a, b, c,...] and produces a new list [F(a), F(b), F(c),...]. filter(P, list) : Applies a boolean function ("predicate") P to each element, and produces a new list of every element k from the list where P(k) was true. reduce(F, list) : Collapses list into a single value by applying F to pairs of elements. F is assumed to accept two of list's values and produce a single value each time.

16 Implementing map ML includes map, but let's think about how it might be implemented by writing our own version. - fun map(F, []) = [] | map(F, first::rest) = F(first) :: map(F, rest); val map = fn : ('a -> 'b) * 'a list -> 'b list - map(abs, [2, ~7, 19, ~1, ~95, 6]); val it = [2,7,19,1,95,6] : int list

17 map exercise Use map to convert a list of int s into their square roots.  Example: turn [4, 9, 1, 2, 16] into [2.0, 3.0, 1.0, , 4.0] Solution: - val L = [4, 9, 1, 2, 16]; - map(Math.sqrt, map(real, L));

18 Implementing filter ML includes List.filter, but let's think about how it might be implemented by writing our own version. - fun filter(P, []) = [] | filter(P, first::rest) = if P(first) then first :: filter(P, rest) else filter(P, rest); val map = fn : ('a -> 'b) * 'a list -> 'b list - fun positive(x) = x > 0; - filter(positive, [2, ~7, 19, ~1, ~95, 6]); val it = [2,19,6] : int list

19 Filter exercise Define a function removeAll that accepts a list L and a value k and produces a new list containing L's elements with all occurrences of k removed. (Use filter.)  Example: removeAll( [2, 9, 2, 2, 7, ~8, 2, 4], 2 ) produces [9, 7, ~8, 4] Solution: fun removeAll(L, k) = let fun equalsK(x) = x = k in filter(equalsK, L) end;

20 Implementing reduce ML includes List.foldl and List.foldr, but let's think about how reduce might be implemented by writing our own version: exception EmptyList; fun reduce(F, []) = raise EmptyList | reduce(F, [value]) = value | reduce(F, first::rest) = F(first, reduce(F, rest)); val reduce = fn : ('a * 'a -> 'a) * 'a list -> 'a

21 reduce exercises Use reduce to compute the sum and product of a list. val L = [2, 5, ~1, 8]; reduce(op +, L); Use reduce to square the elements of a list.  Example: turn [2, 5, ~1, 8] into [4, 25, 1, 64] fun square(x) = x*x; reduce(square, L);