Docsity

Understanding Function Values, Higher-Order Functions, and Predefined Functions in ML, Slides of Advanced Computer Programming

Partial preview of the text

Download Understanding Function Values, Higher-Order Functions, and Predefined Functions in ML and more Slides Advanced Computer Programming in PDF only on Docsity!

A Third Look At ML

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Outline

 More pattern matching

 Function values and anonymous functions

 Higher-order functions and currying

 Predefined higher-order functions

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Match Syntax

 A rule is a piece of ML syntax that looks like this:

 A match consists of one or more rules separated

by a vertical bar, like this:

 Each rule in a match must have the same type of

expression on the right-hand side

 A match is not an expression by itself, but forms a

part of several kinds of ML expressions

Chapter Nine Modern Programming Languages, 2nd ed. 4

< rule > ::= < pattern > => < expression >

< match > ::= < rule > | < rule > ' | ' < match >

Docsity.com

Case Expressions

 The syntax is

 This is a very powerful case construct—unlike

many languages, it does more than just compare

with constants

Chapter Nine Modern Programming Languages, 2nd ed. 5

  • case 1+1 of = 3 => "three" | = 2 => "two" | = _ => "hmm"; val it = "two" : string

< case-expr > ::= case < expression > of < match >

Docsity.com

Generalizes if

 The two expressions above are equivalent

 So if - then - else is really just a special

case of case

Chapter Nine Modern Programming Languages, 2nd ed. 7

if exp 1 then exp 2 else exp 3

case exp 1 of true => exp 2 | false => exp 3

Docsity.com

Outline

 More pattern matching

 Function values and anonymous functions

 Higher-order functions and currying

 Predefined higher-order functions

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Defining Functions

 We have seen the fun notation for defining

new named functions

 You can also define new names for old

functions, using val just as for other kinds

of values:

Chapter Nine Modern Programming Languages, 2nd ed. 10

  • val x = ~; val x = fn : int -> int
  • x 3; val it = ~3 : int

Docsity.com

Function Values

 Functions in ML do not have names

 Just like other kinds of values, function

values may be given one or more names by

binding them to variables

 The fun syntax does two separate things:

  • Creates a new function value
  • Binds that function value to a name

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 11

The fn Syntax

 Another use of the match syntax

 Using fn , we get an expression whose value is an

(anonymous) function

 We can define what fun does in terms of val

and fn

 These two definitions have the same effect:

  • fun f x = x + 2
  • val f = fn x => x + 2

Chapter Nine Modern Programming Languages, 2nd ed. 13

< fun-expr > ::= fn < match >

Docsity.com

Using Anonymous Functions

 One simple application: when you need a

small function in just one place

 Without fn :

 With fn :

Chapter Nine Modern Programming Languages, 2nd ed. 14

  • fun intBefore (a,b) = a < b; val intBefore = fn : int * int -> bool
  • quicksort ([1,4,3,2,5], intBefore); val it = [1,2,3,4,5] : int list
  • quicksort ([1,4,3,2,5], fn (a,b) => a<b); val it = [1,2,3,4,5] : int list
  • quicksort ([1,4,3,2,5], fn (a,b) => a>b); val it = [5,4,3,2,1] : int list

Docsity.com

Outline

 More pattern matching

 Function values and anonymous functions

 Higher-order functions and currying

 Predefined higher-order functions

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 16

Higher-order Functions

 Every function has an order :

  • A function that does not take any functions as parameters, and does not return a function value, has order 1
  • A function that takes a function as a parameter or returns a function value has order n+1 , where n is the order of its highest-order parameter or returned value

 The quicksort we just saw is a second-order

function

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 17

Currying

 We've seen how to get two parameters into

a function by passing a 2-tuple:

fun f (a,b) = a + b;

 Another way is to write a function that takes

the first argument, and returns another

function that takes the second argument:

fun g a = fn b => a+b;

 The general name for this is currying

Chapter Nine Modern Programming Languages, 2nd ed. (^) Docsity.com 19

Curried Addition

 Remember that function application is left-

associative

 So g 2 3 means ((g 2) 3)

Chapter Nine Modern Programming Languages, 2nd ed. 20

  • fun f (a,b) = a+b; val f = fn : int * int -> int
  • fun g a = fn b => a+b; val g = fn : int -> int -> int
  • f(2,3); val it = 5 : int
  • g 2 3; val it = 5 : int

Docsity.com