lec3-2x2

lec3-2x2 - Recap from last time CSE 130 : Fall 2011...

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

View Full Document Right Arrow Icon
CSE 130 : Fall 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 3: Data Types 1 Tuesday, October 4, 2011 Recap from last time 2 Tuesday, October 4, 2011 A shorthand for function binding # let neg = fun f -> fun x -> not ( f x ); # let neg f x = not ( f x ); val neg : (‘a -> bool) -> ‘a -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn; # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; 3 Tuesday, October 4, 2011 Put it together: a “filter” function - let rec filter f l = match l with | [] -> [] | (h::t)-> if f h then h::(filter f t) else (filter f t);; val filter : (‘a->bool)->‘a list->‘a list) = fn If arg “matches” this pattern… …then use this Body Expr # let list1 = [1;31;12;4;7;2;10];; # filter is5lt list1 ;; val it : int list = [31;12;7;10] # filter is5gte list1;; val it : int list = [1;4;2] # filter even list1;; val it : int list = [12;4;2;10] 4 Tuesday, October 4, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Put it together: a “partition” function # let partition f l = (filter f l, filter (neg f) l); val partition :(‘a->bool)->‘a list->‘a list*‘a list = fn # let list1 = [1;31;12;4;7;2;10]; - … # partition is5lt list1 ; val it : (int list * int list) = ([31;12;7;10],[1;2;10] # partition even list1; val it : (int list * int list) = ([12;4;2;10],[1;31;7]) 5 Tuesday, October 4, 2011 A little trick … # 2 <= 3;; val it : bool = true # “ba” <= “ab”;; val it : bool = false # let lt = (<) ;; val it : ‘a -> ‘a -> bool = fn # lt 2 3;; val it : bool = true; # lt “ba” “ab” ;; val it : bool = false; # let is5Lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false; 6 Tuesday, October 4, 2011 Put it together: a “quicksort” function let rec sort xs = match xs with | [] -> [] | (h::t) -> let (l,r) = partition ((<) h) t in (sort l)@(h::(sort r)) ;; Now, lets begin at the beginning … 7 Tuesday, October 4, 2011 Begin at the beginning … 1. Programmer enters expression 2. ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression meaningful type for the expr
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

lec3-2x2 - Recap from last time CSE 130 : Fall 2011...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online