08-OCaml2 - 1 CMSC 330: Organization of Programming...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CMSC 330: Organization of Programming Languages OCaml 2 Data Types & Recursion CMSC 330 2 This Lecture Tuples Polymorphic functions & types User-defined data types Defining recursive functions with let rec Recursive functions CMSC 330 3 OCaml Functions Take One Argument Recall this example It looks like youre passing in two arguments Actually, youre passing in a tuple instead And using pattern matching to extract its contents let plus (x, y) = x + y;; plus (3, 4);; let plus t = match t with (x, y) -> x + y;; plus (3, 4);; CMSC 330 4 Tuples Constructed using (e1, ..., en) Deconstructed using pattern matching Tuples are like C structs But without field labels Allocated on the heap Tuples can be heterogenous Unlike lists, which must be homogenous (1, ["string1"; "string2"]) is a valid tuple CMSC 330 5 Tuples Examples let plusThree (x, y, z) = x+y+z let addOne (x, y, z) = (x+1, y+1, z+1) plusThree (addOne (3,4,5)) = let sum ((a, b), c) = (a+c, b+c) sum ((1, 2), 3) = let plusFirstTwo (x::y::_, a) = (x+a, y+a) plusFirstTwo ([1; 2; 3], 4) = 15 (5,6) (4,5) CMSC 330 6 Tuples More Examples let tls (_::xs, _::ys) = (xs, ys) tls ([1;2;3],[4;5;6;7]) = Remember Semicolon for lists Comma for tuples Example [1, 2] = [(1, 2)] = a list of size one (1; 2) = a syntax error ([2;3],[5;6;7]) 2 CMSC 330 7 Another Tuple Example Given let f l = match l with x::(_::y) -> (x,y) What is the value of f [1;2;3;4] Possibilities ([1],[3]) (1,3) (1,[3]) (1,4) (1,[3;4]) CMSC 330 8 List and Tuple Types Tuple types use * to separate components Examples (1,2) : (1,"string",3.5) : (1,["a";"b"],'c'): [(1,2)] : [(1,2);(3, 4)] : [(1,2);(1,2,3)] : int * int int * string * float int * string list * char (int * int) list (int * int) list error CMSC 330 9 Polymorphic Functions Some functions require specific list types let plusFirstTwo (x::y::_, a) = (x + a, y + a) plusFirstTwo : int list * int -> (int * int) But other functions work for a list of any type let hd (h::_) = h hd [1; 2; 3] (* returns 1 *) hd ["a"; "b"; "c"] (* returns "a" *) These functions are polymorphic CMSC 330 10 Polymorphic Types OCaml gives such functions polymorphic types hd : 'a list -> 'a Read as & Function takes a list of any element type 'a & And returns something of that type Example let tl (_::t) = t tl : 'a list -> 'a list CMSC 330 11 Polymorphic Types (cont.) More Examples let swap (x, y) = (y, x) swap : 'a * 'b -> 'b * 'a let tls (_::xs, _::ys) = (xs, ys)...
View Full 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

08-OCaml2 - 1 CMSC 330: Organization of Programming...

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