Docsity

OCaml Tuples and Polymorphic Functions, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 330: Organization of
Programming Languages
OCaml Features
CMSC 330 2
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 3
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 4
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 5
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])
CMSC 330 6
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])
pf3
pf4

Partial preview of the text

Download OCaml Tuples and Polymorphic Functions and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

OCaml Features

CMSC 330 2

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 3

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 4

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) =

CMSC 330 5

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])

CMSC 330 6

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;4])

CMSC 330 7

List and Tuple Types

Tuple types use * to separate components

Examples

  • (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 8

Type declarations

type can be used to create new names for types

  • Useful for combinations of lists and tuples

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

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

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

Would yield this error message

  • This pattern matches values of type 'a * 'b * 'c but is here used to match values of type 'd * 'e

Tuples of different size have different types

  • Thus never more than one match case with tuples

CMSC 330 19

Examples – Let

let x = 1 in x + 1 ; x ;;

  • (* 1 – ; has higher precedence than let … in *)

(let x = 1 in x + 1) ; x;;

  • (* Unbound value x *)

let x = 4 in let x = x + 1 in x ;;

CMSC 330 20

Let – More Examples

let f n = 10;;

let f n = if n = 0 then 1 else n * f (n – 1);;

  • f 0;;
  • f 1;;

let f x = f x;;

  • (* Unbound value f *)