Docsity

Functional Programming with OCaml: Pattern Matching and Tuples, Study notes of Programming Languages

Partial preview of the text

Download Functional Programming with OCaml: Pattern Matching and Tuples and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Functional Programming with OCaml

Review

• function declaration

– let [rec] =

• types

– int, bool, string

• lists

– [i1; i2; i3] or i1::i2::i3::[]

Example

match e with p 1 -> e 1 | ... | pn -> en

let is_empty l = match l with

[] -> true

| (h::t) -> false

is_empty [] (* evaluates to true *)

is_empty [1] (* evaluates to false *)

is_empty [1;2;3] (* evaluates to false *)

Pattern Matching (cont’d)

  • (^) let hd l = match l with (h::t) -> h
    • (^) hd [1;2;3] (* evaluates to 1 *)
  • (^) let hd l = match l with (h::_) -> h
    • (^) hd [] (* error! no pattern matches *)
  • (^) let tl l = match l with (h::t) -> t
    • (^) tl [1;2;3] (* evaluates to [2; 3] *)

Example

match e with p 1 -> e 1 | ... | pn -> en

let is_empty l = match l with

[] -> true

| (h::t) -> false

is_empty [] (* evaluates to true *)

is_empty [1] (* evaluates to false *)

is_empty [1;2;3] (* evaluates to false *)

More Examples

  • (^) let f l =

match l with (h1::(h2::_)) -> h1 + h

  • (^) f [1;2;3]
  • (^) (* evaluates to 3 *)
  • (^) let g l =

match l with [h1; h2] -> h1 + h

  • (^) g [1; 2]
  • (^) (* evaluates to 3 *)
  • (^) g [1; 2; 3]
  • (^) (* error! no pattern matches *)

Two element

list [h1;h2]

Pattern Matching Lists of Lists

• You can do pattern matching on these as well

• Examples

  • (^) let addFirsts ((x::) :: (y::) :: _) = x + y
    • (^) addFirsts [ [1; 2; 3]; [4; 5]; [7; 8; 9] ] = 5
  • (^) let addFirstSecond ((x::)::(::y::)::) = x + y
    • (^) addFirstSecond [ [1; 2; 3]; [4; 5]; [7; 8; 9] ] = 6

• Note: You probably won’t do this much or at all

– You’ll mostly write recursive functions over lists

– We’ll see that soon

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

• Tuples are constructed using (e 1 , ..., en)

– They’re like C structs but without field labels, and

allocated on the heap

– Unlike lists, tuples do not need to be homogenous

– E.g., (1, ["string1"; "string2"]) is a valid tuple

• Tuples are deconstructed using pattern matching

let plus (x, y) = x + y;; plus (3, 4);;

Another Example

• let f l = match l with x::(_::y) -> (x,y)

• What is f [1;2;3;4]?

(1,[3;4])

List and Tuple Types

• Tuple types use * to separate components

• Examples

  • (^) (1, 2) : int * int
  • (^) (1, "string", 3.5) : int * string * float
  • (^) (1, ["a"; "b"], 'c') :
  • (^) [(1,2)] :
  • (^) [(1, 2); (3, 4)] :
  • (^) [(1,2); (1,2,3)] :

Type declarations

• type can be used to create new names for types

– useful for combinations of lists and tuples

– like “typedef” in C

• Examples

type my_type = int * (int list)

(3, [1; 2]) : my_type

type my_type2 = int * char * (int * float)

(3, ‘a’, (5, 3.0)) : my_type

Polymorphic Types

• Some functions we saw require specific list types

  • (^) let plusFirstTwo (x::y::_, a) = (x + a, y + a)
  • (^) plusFirstTwo : int list * int -> (int * int)

• But other functions work for any list

  • (^) let hd (h::_) = h
  • (^) hd [1; 2; 3] (* returns 1 *)
  • (^) hd ["a"; "b"; "c"] (* returns "a" *)

• OCaml gives such functions polymorphic types

  • (^) hd : 'a list -> 'a

– this says the function takes a list of any element type

'a, and returns something of that type

Tuples Are a Fixed Size

let foo x = match x with

(a, b) -> a + b

| (a, b, c) -> a + b + c;;

This pattern matches values of type 'a * 'b * 'c

but is here used to match values of type 'd * 'e

• Thus there's never more than one match case

with tuples

– How’s this instead?

let foo (a, b) = a + b

Conditionals

• Use if...then...else like C/Java

– No parentheses and no end

if grade >= 90 then print_string "You got an A" else if grade >= 80 then print_string "You got a B" else if grade >= 70 then print_string "You got a C" else print_string "You’re not doing so well"