09-OCaml3

# 09-OCaml3 - This Lecture CMSC 330 Organization of...

This preview shows pages 1–3. Sign up to view the full content.

1 CMSC 330: Organization of Programming Languages OCaml 3 Higher Order Functions, Closures, Currying CMSC 330 2 This Lecture Higher order functions • Map, fold Currying CMSC 330 3 Higher-Order Functions In OCaml you can pass functions as arguments, and return functions as results let plus_three x = x + 3 let twice (f, z) = f (f z) twice (plus_three, 5) // twice : ('a->'a) * 'a -> 'a let plus_four x = x + 4 let pick_fn n = if n > 0 then plus_three else plus_four (pick_fn 5) 0 // pick_fn : int -> (int->int) = 3 = 11 CMSC 330 4 The map Function Let’s write the map function (just like Ruby' s collect ) • Takes a function and a list, applies the function to each element of the list, and returns a list of the results let add_one x = x + 1 let negate x = -x map (add_one, [1; 2; 3]) map (negate, [9; -5; 0]) let rec map (f, l) = match l with [] -> [] | (h::t) -> (f h)::(map (f, t)) = [2; 3; 4] = [-9; 5; 0] CMSC 330 5 The map Function (cont.) What is the type of the map function? ('a -> 'b) * 'a list -> 'b list f l let rec map (f, l) = match l with [] -> [] | (h::t) -> (f h)::(map (f, t)) CMSC 330 6 Anonymous Functions Passing functions around is very common • So often we don’t want to bother to give them names Use fun to make a function with no name twice ((fun x -> x + 3), 5) map ((fun x -> x+1), [1; 2; 3]) fun x -> x + 3 Parameter Body = [2; 3; 4] = 11

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CMSC 330 7 Pattern Matching with fun match can be used within fun map ((fun l -> match l with (h::_) -> h), [ [1; 2; 3]; [4; 5; 6; 7]; [8; 9] ]) But use named functions for complicated matches May use standard pattern matching abbreviations map ((fun (x, y) -> x+y), [(1,2); (3,4)]) = [1; 4; 8] = [3; 7] CMSC 330 8 All Functions Are Anonymous Functions are first-class, so you can bind them to other names as you like let f x = x + 3 let g = f g 5 In fact, let for functions is just shorthand let f x = body stands for let f = fun x -> body = 8 CMSC 330 9 Examples – Anonymous Functions let next x = x + 1 • Short for let next = fun x -> x + 1 let plus (x, y) = x + y • Short for let plus = fun (x, y) -> x + y • Which is short for let plus = fun z -> (match z with (x, y) -> x + y) CMSC 330 10 Examples – Anonymous Functions let rec fact n = if n = 0 then 1 else n * fact (n-1) • Short for let rec fact = fun n -> (if n = 0 then 1 else n * fact (n-1)) CMSC 330 11 Common pattern • Iterate through list and apply function to each element, keeping track of partial results computed so far
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

### Page1 / 7

09-OCaml3 - This Lecture CMSC 330 Organization of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online