A Third Look At ML
Chapter Nine Modern Programming Languages, 2nd ed. 1
A Third Look At ML Chapter Nine Modern Programming Languages, 2nd - - PowerPoint PPT Presentation
A Third Look At ML Chapter Nine Modern Programming Languages, 2nd ed. 1 Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order functions Chapter Nine
Chapter Nine Modern Programming Languages, 2nd ed. 1
Chapter Nine Modern Programming Languages, 2nd ed. 2
– fun f 0 = "zero"
– case n of
Chapter Nine Modern Programming Languages, 2nd ed. 3
A rule is a piece of ML syntax that looks like this: A match consists of one or more rules separated
Each rule in a match must have the same type of
A match is not an expression by itself, but forms a
Chapter Nine Modern Programming Languages, 2nd ed. 4
The syntax is This is a very powerful case construct—unlike
Chapter Nine Modern Programming Languages, 2nd ed. 5
Chapter Nine Modern Programming Languages, 2nd ed. 6
Chapter Nine Modern Programming Languages, 2nd ed. 7
Chapter Nine Modern Programming Languages, 2nd ed. 8
Chapter Nine Modern Programming Languages, 2nd ed. 9
Chapter Nine Modern Programming Languages, 2nd ed. 10
– Creates a new function value – Binds that function value to a name
Chapter Nine Modern Programming Languages, 2nd ed. 11
Chapter Nine Modern Programming Languages, 2nd ed. 12
Another use of the match syntax Using fn, we get an expression whose value is an
We can define what fun does in terms of val
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
Chapter Nine Modern Programming Languages, 2nd ed. 14
Chapter Nine Modern Programming Languages, 2nd ed. 15
val it = fn : int * int -> int
val it = [1,2,3,4,5] : int list
Chapter Nine Modern Programming Languages, 2nd ed. 16
Every function has an order:
– A function that does not take any functions as
– A function that takes a function as a parameter or
The quicksort we just saw is a second-order
Chapter Nine Modern Programming Languages, 2nd ed. 17
Chapter Nine Modern Programming Languages, 2nd ed. 18
Chapter Nine Modern Programming Languages, 2nd ed. 19
Remember that function application is left-
So g 2 3 means ((g 2) 3)
Chapter Nine Modern Programming Languages, 2nd ed. 20
Chapter Nine Modern Programming Languages, 2nd ed. 21
Chapter Nine Modern Programming Languages, 2nd ed. 22
val it = [1,2,3,4,5] : int list
val sortBackward = fn : int list -> int list
val it = [5,4,3,2,1] : int list
Chapter Nine Modern Programming Languages, 2nd ed. 23
There is a much simpler notation for currying (on
The long notation we have used so far makes the
But as long as you understand how it works, the
Chapter Nine Modern Programming Languages, 2nd ed. 24
Chapter Nine Modern Programming Languages, 2nd ed. 25
Chapter Nine Modern Programming Languages, 2nd ed. 26
– map – foldr – foldl
Chapter Nine Modern Programming Languages, 2nd ed. 27
Chapter Nine Modern Programming Languages, 2nd ed. 28
Chapter Nine Modern Programming Languages, 2nd ed. 29
val it = fn : ('a -> 'b) -> 'a list -> 'b list
val f = fn : (int * int) list -> int list
val it = [3,7] : int list
Used to combine all the elements of a list For example, to add up all the elements of a list x,
It takes a function f, a starting value c, and a list x
So foldr (op +) 0 [1,2,3,4] evaluates
Chapter Nine Modern Programming Languages, 2nd ed. 30
Chapter Nine Modern Programming Languages, 2nd ed. 31
val it = 10 : int
val it = 24 : int
val it = "abcdefghi" : string
val it = [1,2,3,4,5] : int list
Chapter Nine Modern Programming Languages, 2nd ed. 32
val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
val it = fn : int -> int list -> int
val it = fn : int list -> int
val addup = fn : int list -> int
val it = 15 : int
Used to combine all the elements of a list Same results as foldr in some cases
Chapter Nine Modern Programming Languages, 2nd ed. 33
val it = 10 : int
val it = 24 : int
To add up all the elements of a list x, we could
It takes a function f, a starting value c, and a list x
So foldl (op +) 0 [1,2,3,4] evaluates
Remember, foldr did 1+(2+(3+(4+0)))=10
Chapter Nine Modern Programming Languages, 2nd ed. 34
foldl starts at the left, foldr starts at the
Difference does not matter when the function is
For other operations, it does matter
Chapter Nine Modern Programming Languages, 2nd ed. 35
val it = "abcdefghi" : string
val it = "ghidefabc" : string
val it = ~2 : int
val it = 2 : int