390 likes | 494 Views
Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Tuples. - val s = ( 5 , "hi" , 3.2 ) ; val s = (5,"hi",3.2) : int * string * real - (* Pattern matching with tuples *) - val (a,b,c) = s; val a = 5 : int val b = "hi" : string
E N D
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635
Tuples - val s = (5,"hi",3.2); val s = (5,"hi",3.2) : int * string * real - (* Pattern matching with tuples *) - val (a,b,c) = s; val a = 5 : int val b = "hi" : string val c = 3.2 : real
Tuples - val d = ((1,4,62),("bye",15),73.95); val d = ((1,4,62),("bye",15),73.95) : (int * int * int) * (string * int) * real - val (p,(st,_),_) = d; val p = (1,4,6) : int * int * int val st = "bye" : string
Tuples - fun fst_of_3 (x,_,_) = x; val fst_of_3 = fn : 'a * 'b * 'c -> 'a - s; val it = (5,"hi",3.2) : int * string * real - fst_of_3 s; val it = 5 : int - fst_of_3 d; val it = (1,4,62) : int * int * int
Records - val teacher = {Name = "Elsa L. Gunter", ss = (119,73,6244), age = 102}; val teacher = {Name="Elsa L. Gunter",age=102,ss=(119,73,6244)} : {Name:string, age:int, ss:int * int * int}
Records - val {ss = (s1,s2,s3), Name = elsa, age = years} = teacher; val elsa = "Elsa L. Gunter" : string val years = 102 : int val s1 = 119 : int val s2 = 73 : int val s3 = 6244 : int
Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});
Records and Tuples val q = (280, {instructor={Name="Elsa L. Gunter", age=102,ss=(119,73,6244)}, student={Name="Joseph Martins", age=19,ss=(325,40,1276)}}) : int * {instructor:{Name:string, age:int, ss:int * int * int}, student:{Name:string, age:int, ss:int * int * int}}
Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool - val h = #2 s; val h = "hi" : string
Lists - val fib5 = [8,5,3,2,1,1]; val fib5 = [8,5,3,2,1,1] : int list - val fib6 = 13 :: fib5; val fib6 = [13,8,5,3,2,1,1] : int list - (8::5::3::2::1::1::[]) = fib5; val it = true : bool - fib5 @ fib6; val it = [8,5,3,2,1,1,13,8,5,3,2,1,...] : int list
Functions Over Lists - fun double_up [] = [] = | double_up (x :: xs) = (x :: x :: double_up xs); val double_up = fn : 'a list -> 'a list - val fib5_2 = double_up fib5; val fib5_2 = [8,8,5,5,3,3,2,2,1,1,1,1] : int list
Functions Over Lists - val silly = double_up ["hi", "there"]; val silly = ["hi","hi","there","there"] : string list - fun rev [] = [] = | rev (x::xs) = rev xs @ [x]; val rev = fn : 'a list -> 'a list - rev silly; val it = ["there","there","hi","hi"] : string list
Functions Over Lists - fun map f [] = [] = | map f (h::t) = (f h) :: (map f t); val map = fn : ('a -> 'b) -> 'a list -> 'b list - map plus_two fib5; val it = [10,7,5,4,3,3] : int list - map (fn x => x - 1) fib6; val it = [12,7,4,2,1,0,0] : int list
Cases and Scoped Bindings - fun fib_seq n = = if n < 0 then [] else =(case fib_seq (n - 1) =of [] => [1] =| (_::[]) => [1,1] = | result as (m1 :: m2 :: _) = => (m1 + m2) :: result); val fib_seq = fn : int -> int list
Cases and Scoped Bindings - fib_seq 6; val it = [13,8,5,3,2,1,1] : int list
User-defined Datatypes -datatype weekday = Monday | Tuesday | Wednesday =| Thursday | Friday | Saturday | Sunday; datatype weekday = Friday | Monday | Saturday | Sunday | Thursday | Tuesday | Wednesday
Functions Over Datatypes - fun day_after Monday = Tuesday = | day_after Tuesday = Wednesday = | day_after Wednesday = Thursday = | day_after Thursday = Friday = | day_after Friday = Saturday = | day_after Saturday = Sunday = | day_after Sunday = Monday; val day_after = fn : weekday -> weekday
Functions Over Datatypes - fun days_later 0 day = day = | days_later n day = = if n > 0 = then day_after (days_later (n - 1) day) = else days_later (n + 7) day; val days_later = fn : int -> weekday -> weekday - days_later ~1 Wednesday; val it = Tuesday : weekday - days_later ~4 Monday; val it = Thursday : weekday
More Datatypes - datatype Int_Bin_Tree = = Leaf of int| Node of (Int_Bin_Tree * Int_Bin_Tree); datatype Int_Bin_Tree = Leaf of int | Node of Int_Bin_Tree * Int_Bin_Tree - datatype 'a option = NONE | SOME of 'a; datatype 'a option = NONE | SOME of 'a
Datatype Values - val bin_tree = Node(Node(Leaf 3, Leaf 6),Leaf ~7); val bin_tree = Node (Node (Leaf #,Leaf #),Leaf ~7) : Int_Bin_Tree
Functions over Datatypes - fun first_leaf_value (Leaf n) = n = | first_leaf_value (Node (left_tree, right_tree)) = = first_leaf_value left_tree; val first_leaf_value = fn : Int_Bin_Tree -> int - val first = first_leaf_value bin_tree; val first = 3 : int
Functions over Datatypes - fun is_neg n = n < 0; val is_neg = fn : int -> bool - fun first_neg (Leaf n) = = if is_neg n then SOME n else NONE = | first_neg (Node(t1,t2)) = = (case first_neg t1 = of SOME n => SOME n = | NONE => first_neg t2);
Functions over Datatypes val first_neg = fn : Int_Bin_Tree -> int option - val n = first_neg bin_tree; val n = SOME ~7 : int option
Additional Topics Topics that follow won’t be covered in class You won’t be tested on them, or need them for homework. They are here in case you are interested
Scoped Bindings - val fib7 = =let val (m1::m2::_) = fib6 =in (m1 + m2) :: fib6 =end; stdIn:26.7-26.29 Warning: binding not exhaustive m1 :: m2 :: _ = ... val fib7 = [21,13,8,5,3,2,1,1] : int list
Scoped Bindings - local = fun fib_pair 0 = (0,1) = | fib_pair n = = if n < 0 then (0,0) else = let val (m1,m2) = fib_pair (n -1) = in (m2, m1+m2) = end =in
Scoped Bindings = fun fib n = #2 (fib_pair n) =end; val fib = fn : int -> int - fib 7; val it = 21 : int
Exceptions -exception Cant_handle of string; exception Cant_handle of string - fun head [] = raise Cant_handle "head" = | head (h::tl) = h = fun tail [] = raise Cant_handle "tail" = | tail (_::tl) = tl; val head = fn : 'a list -> 'a val tail = fn : 'a list -> 'a list
Exceptions - fun fib_seq 0 = [1] = | fib_seq 1 = [1,1] = | fib_seq n = = if n < 0 then raise Cant_handle "fib_seq" = else = let val result as (m1::m2::_) = fib_seq (n - 1) = in (m1 + m2)::result = end;
Exceptions stdIn:130.6-130.49 Warning: binding not exhaustive result as m1 :: m2 :: _ = ... val fib_seq = fn : int -> int list - fib_seq (~15); uncaught exception Cant_handle raised at: stdIn:128.25-128.46
Exceptions - fib_seq (~2) handle Cant_handle _ => []; val it = [] : int list - fun new_fib_seq n = fib_seq n handle Cant_handle "fib_seq" => []; val new_fib_seq = fn : int -> int list - new_fib_seq (~3); val it = [] : int list
Exceptions - exception Cant_happen; exception Cant_happen - fun fib_seq 0 = [1] = | fib_seq 1 = [1,1] = | fib_seq n = = if n < 0 then raise Cant_handle "fib_seq"
Exceptions = else = (case fib_seq (n - 1) of = result as (r1 :: r2 :: _) => r1 + r2 :: result = | _ => raise Cant_happen); val fib_seq = fn : int -> int list
Side Effects and Imperative Programming - local = val cell = ref 0 = in = fun get_count () = = let val count = !cell = val _ = cell := count + 1 = in count = end = end;
Side Effects and Imperative Programming val get_count = fn : unit -> int - get_count(); val it = 0 : int - get_count(); val it = 1 : int - get_count(); val it = 2 : int - (* Example of an integer stream *)
Side Effects and Imperative Programming - local = val cell = ref (~1) = in = fun get_count () = = (cell := ((!cell) + 1); = !cell) = end; val get_count = fn : unit -> int
Side Effects and Imperative Programming - get_count(); val it = 0 : int - get_count(); val it = 1 : int - get_count(); val it = 2 : int
Side Effects and Imperative Programming - local = val cell = ref (~1) = in = fun get_count () = = let val _ = cell := !cell + 1 = in !cell = end = end; val get_count = fn : unit -> int