{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-OCaml2

# 08-OCaml2 - 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 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 you’re passing in two arguments Actually, you’re 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])

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

View Full Document
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) tls : 'a list * 'b list -> 'a list * 'b list CMSC 330 12 Tuples Are a Fixed Size This OCaml definition # let foo x = match x with
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

08-OCaml2 - 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