Docsity

Function Declarations and Pattern Matching in ML: Lecture Notes from CSE 452, Fall 2008 - , Study notes of Programming Languages

Toggle sidebar
bg1
L. Dillon, CSE 452, Fall 2008 1
General form of a declaration: •
val <pattern> = <exp>;
Can• beusedtobindmultipleidentifierstovalues:
Patterns
- val (height, width) = (20, 10);
val height = 20 : int
val width = 10 : int
- val {name = n, age = a} = {age = 10, name = “Big Bird”};
val a = 10 : int
val n = “Big Bird” : string
- val h::t = [3, 2, 1, 0];
stdIn:18.5-18.24 Warning: binding not exhaustive
h :: t = ...
val h = 3 : int
val t = [2,1,0] : int list
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Function Declarations and Pattern Matching in ML: Lecture Notes from CSE 452, Fall 2008 - and more Study notes Programming Languages in PDF only on Docsity!

• General form of a declaration:

val = ;

• Can be used to bind multiple identifiers to values:

Patterns

  • val (height, width) = (20, 10); val height = 20 : int val width = 10 : int
  • val {name = n, age = a} = {age = 10, name = “Big Bird”}; val a = 10 : int val n = “Big Bird” : string
  • val h::t = [3, 2, 1, 0]; stdIn:18.5-18.24 Warning: binding not exhaustive h :: t = ... val h = 3 : int val t = [2,1,0] : int list

• General form of a declaration:

val = ;

• Can be used to bind multiple identifiers to values:

Patterns

  • val (height, width) = (20, 10); val height = 20 : int val width = 10 : int
  • val {name = n, age = a} = {age = 10, name = “Big Bird”}; val a = 10 : int val n = “Big Bird” : string
  • val h::t = [3, 2, 1, 0]; stdIn:18.5-18.24 Warning: binding not exhaustive h :: t = ... val h = 3 : int val t = [2,1,0] : int list

• General form of function declaration:

fun f() = |... |f() =

• Use for more succinct/understandable declarations:

Patterns in function declarations

  • fun sum [] = 0 = | sum (x::xr) = x + sum xr; val sum = fn : int list -> int
  • sum nil; val it = 0 : int
  • sum [1, 2, 3, 4, = 5, 6, 7, 8]; val it = 36 : int
    • fun sum y = = if (y = []) then 0 = else = let val x = hd y = val xr = tl y = in = x + sum xr = end; val sum = fn : int list -> int

• General form of function declaration:

fun f() = |... |f() =

• Use for more succinct/understandable declarations:

Patterns in function declarations

  • fun sum [] = 0 = | sum (x::xr) = x + sum xr; val sum = fn : int list -> int
  • sum nil; val it = 0 : int
  • sum [1, 2, 3, 4, = 5, 6, 7, 8]; val it = 36 : int
    • fun sum y = = if (y = []) then 0 = else = let val x = hd y = val xr = tl y = in = x + sum xr = end; val sum = fn : int list -> int

• General form of function declaration:

fun f() = |... |f() =

• Use for more succinct/understandable declarations:

Patterns in function declarations

  • fun sum [] = 0 = | sum (x::xr) = x + sum xr; val sum = fn : int list -> int
  • sum nil; val it = 0 : int
  • sum [1, 2, 3, 4, = 5, 6, 7, 8]; val it = 36 : int
    • fun sum y = = if (y = []) then 0 = else = let val x = hd y = val xr = tl y = in = x + sum xr = end; val sum = fn : int list -> int
  • fun taxiDistance ((x1,y1),(x2,y2)) = = abs(x1-x2) + abs(y1-y2); val taxiDistance = fn : (int * int) * (int * int) -> int
  • taxiDistance ((~2, ~1), (3, ~6)); val it = 10 : int
  • fun second (x1::x2::rest) = x2; stdIn:32.5-32.31 Warning: match nonexhaustive x1 :: x2 :: rest => ... val second = fn : ‘a list -> ‘a
  • second [“a”, “b”, “c”, “d”]; val it = “b” : string
  • second [“a”]; uncaught exception Match [nonexhaustive match failure] raised at: stdIn:32.

Type and data-type declarations

• See dataTypeExamples.sml

General terminology: L-values and R-values

• Assignment y := x+

− Identifier on left refers to a memory location, called an L-value − Identifier on right refers to contents, called an R-value

• Variables

− Most languages ◊ A variable names a storage location ◊ Contents of location can be read, can be changed − ML reference cell ◊ A mutable cell is another type of value ◊ Explicit operations to read contents or change contents ◊ Separates naming (declaration of identifiers) from “variables”

Variables and assignment

General terminology: L-values and R-values

• Assignment y := x+

− Identifier on left refers to a memory location, called an L-value − Identifier on right refers to contents, called an R-value

• Variables

− Most languages ◊ A variable names a storage location ◊ Contents of location can be read, can be changed − ML reference cell ◊ A mutable cell is another type of value ◊ Explicit operations to read contents or change contents ◊ Separates naming (declaration of identifiers) from “variables”

Variables and assignment

ML reference cells

• Different types for location and contents

− x : int; (* non-assignable integer value ) − y : int ref; ( location whose contents must be integer ) − !y : int; ( the contents of location y ) − ref x : int ref ( constructs new cell initialized to x *)

• ML assignment

− operator := is applied to memory cell and new contents

• Examples

− y := x+3; (* place value of x+3 in cell y; requires x:int and y:int ref ) − y := !y + 3; ( add 3 to contents of y, store in location y; requires y:int ref*)

ML imperative constructs

ML reference cells

• Different types for location and contents

− x : int; (* non-assignable integer value ) − y : int ref; ( location whose contents must be integer ) − !y : int; ( the contents of location y ) − ref x : int ref ( constructs new cell initialized to x *)

• ML assignment

− operator := is applied to memory cell and new contents

• Examples

− y := x+3; (* place value of x+3 in cell y; requires x:int and y:int ref ) − y := !y + 3; ( add 3 to contents of y, store in location y; requires y:int ref*)

ML imperative constructs

ML reference cells

• Different types for location and contents

− x : int; (* non-assignable integer value ) − y : int ref; ( location whose contents must be integer ) − !y : int; ( the contents of location y ) − ref x : int ref ( constructs new cell initialized to x *)

• ML assignment

− operator := is applied to memory cell and new contents

• Examples

− y := x+3; (* place value of x+3 in cell y; requires x:int and y:int ref ) − y := !y + 3; ( add 3 to contents of y, store in location y; requires y:int ref*)

ML imperative constructs

  • val ctr = ref 0; val ctr = ref 0 : int ref
  • fun inc(x) = x := !x + 1; val inc = fn : int ref -> unit
  • !ctr; val it = 0 : int
  • inc ctr; val it = () : unit
  • ctr; val it = ref 1 : int ref
  • !ctr; val it = 1 : int
    • val ctr = 0; val ctr = 0 : int
    • fun inc(x) = x + 1; val inc = fn : int -> int
    • inc ctr; val it = 1 : int
    • ctr; val it = 0 : int ctr: 0 ctr: 1 inc
  • val ctr = ref 0; val ctr = ref 0 : int ref
  • fun inc(x) = x := !x + 1; val inc = fn : int ref -> unit
  • !ctr; val it = 0 : int
  • inc ctr; val it = () : unit
  • ctr; val it = ref 1 : int ref
  • !ctr; val it = 1 : int
    • val ctr = 0; val ctr = 0 : int
    • fun inc(x) = x + 1; val inc = fn : int -> int
    • inc ctr; val it = 1 : int
    • ctr; val it = 0 : int