lec7-2x2

# lec7-2x2 - CSE 130: Fall 2011 Programming Languages Lecture...

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

CSE 130: Fall 2011 Programming Languages Lecture 7: Polymorphism Ranjit Jhala UC San Diego Polymorphism (or, whats the deal with ’a) News • Midterm Nov 1 – In class – Open book etc. Practice materials on Webpage – Practice materials on Webpage Another pattern: Accumulation let max x y = if x > y then x else y ; let listMax l = let rec help cur l = match l with [] -> cur | h :: t -> help (max cur h) t let concat l = in helper 0 l ;; let rec help cur l = match l with [] -> cur | h :: t -> help ( cur^ h) t in helper “” l ;; What’s the pattern?

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

View Full Document
Examples of fold let listMax = Currying! This is a function! fold max 0 let concat = let multiplier = Currying! This is a function! Currying! This is a function! fold (^) “” fold (*) 1 Pick correct base case! Funcs taking/returning funcs Identify common computation “patterns” • Filter values in a set, list , tree … • Iterate a function over a set, list , tree … • Accumulate some value over a collection Pull out (factor) “common” code: • Computation Patterns • Re-use in many different situations map fold Another fun function: “pipe” let pipe x f = f x let (|>) x f = f x Compute the sum of squares of numbers in a list ? let sumOfSquares xs = xs |> map (fun x -> x * x) |> fold_left (+) 0 Tail Rec ? Another Pattern: Synchronize acquire(lock); S; release(lock); synchronize(lock){ S; } let synchronize lock s = acquire(lock); s(); release(lock) synchronize(fun () -> … )
Funcs taking/returning funcs Identify common computation “patterns” • Filter values in a set, list , tree … • Convert a function over a set, list , tree … map • Iterate a function over a set, list , tree … • Accumulate some value over a collection Pull out (factor) “common” code: • Computation Patterns • Re-use in many different situations fold Functions are “first-class” values • Arguments, return values, bindings … • What are the benefits ? Parameterized, similar functions Iterator, Accumul, Reuse computation Creating, (Returning) Functions Using, (Taking) Functions (e.g. Testers) pattern w/o exposing local info Compose Functions: Flexible way to build Complex functions from primitives. Functions are “first-class” values

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/11/2011 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

### Page1 / 11

lec7-2x2 - CSE 130: Fall 2011 Programming Languages Lecture...

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

View Full Document
Ask a homework question - tutors are online