03-Even-More-OCaml

03-Even-More-OCaml - CS 421 Lecture 3: Even More OCaml...

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

CS 421 Lecture 3: Even More OCaml s Announcements s Lecture outline s Type declaration in OCaml s Trees s Polymorphic types s Abstract syntax 6/4/2009 1

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

View Full Document
6/4/2009 2 Announcements s Reminder: no “live” lectures next Monday & Tuesday (June 8, 9) s Pre-recorded lecture videos have been posted on the web site s Reminder: limited course staff availability this weekend s Friday – Sunday you are on your own! s MP2 has been posted s Due 1:00PM Wednesday, June 10
Brief review s Tuples s Fixed-size, heterogeneous collections s Ex: (“hello”, “cs”, 421) s Type: string * string * int s Pairs s Tuples with two values s fst, snd functions s Lists s Variable-size, homogeneous collections s Ex: [1; 2; 3; 4; 5] s Type: int list s :: - cons, @ - append s [1; 2] @ (3 :: [4; 5]) = [1; 2; 3; 4; 5] 6/4/2009 3

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

View Full Document
Brief review s Pattern matching s let incr_second_of_3 (x,y,z) = y+1;; s Type: `a * int * `c -> int s let sum_pair p = (fst p) + (snd p) s Type: int * int -> int s Match expressions s Pattern matching with choice among alternate options s let rec is_even lst = match lst with [] -> true | x::[] -> false | x::y::ys -> is_even ys s Type: `a list -> boolean 6/4/2009 4
Type declaration in OCaml s First, type expressions are: s te = int | string | unit | … | te list | te * te * … * te 6/4/2009 5

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

View Full Document
s type t = te s After this, t is an abbreviation for te s Similar to “let” syntax for names s type t = C 1 [of te 1 ] | … | C n [of te n ] s Where C 1 , …, C n are constructor names – names that start with a capital letter s Values of type t are created by applying C 1 to value of type te 1 , or C 2 to value of type te 1 , etc. 6/4/2009
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/01/2009 for the course CS CS421 taught by Professor Kirillmechitov during the Summer '09 term at University of Illinois, Urbana Champaign.

Page1 / 20

03-Even-More-OCaml - CS 421 Lecture 3: Even More OCaml...

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

View Full Document
Ask a homework question - tutors are online