programming language concepts (cis 635)
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/1.jpg)
Programming Language Concepts (CIS 635)
Elsa L Gunter
4303 GITC
NJIT, www.cs.njit.edu/~elsa/635
![Page 2: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/2.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 3: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/3.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 4: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/4.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 5: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/5.jpg)
Copyright 2002 Elsa L. Gunter
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}
![Page 6: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/6.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 7: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/7.jpg)
Copyright 2002 Elsa L. Gunter
Records and Tuples
- val q = (280,{student = {Name = "Joseph Martins",
= ss = (325,40,1276),
= age = 19},
= instructor = teacher});
![Page 8: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/8.jpg)
Copyright 2002 Elsa L. Gunter
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}}
![Page 9: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/9.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 10: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/10.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 11: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/11.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 12: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/12.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 13: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/13.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 14: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/14.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 15: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/15.jpg)
Copyright 2002 Elsa L. Gunter
Cases and Scoped Bindings
- fib_seq 6;
val it = [13,8,5,3,2,1,1] : int list
![Page 16: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/16.jpg)
Copyright 2002 Elsa L. Gunter
User-defined Datatypes
- datatype weekday = Monday | Tuesday | Wednesday
= | Thursday | Friday | Saturday | Sunday;
datatype weekday
= Friday | Monday | Saturday | Sunday | Thursday | Tuesday | Wednesday
![Page 17: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/17.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 18: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/18.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 19: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/19.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 20: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/20.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 21: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/21.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 22: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/22.jpg)
Copyright 2002 Elsa L. Gunter
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);
![Page 23: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/23.jpg)
Copyright 2002 Elsa L. Gunter
Functions over Datatypes
val first_neg = fn : Int_Bin_Tree -> int option
- val n = first_neg bin_tree;
val n = SOME ~7 : int option
![Page 24: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/24.jpg)
Copyright 2002 Elsa L. Gunter
![Page 25: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/25.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 26: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/26.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 27: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/27.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 28: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/28.jpg)
Copyright 2002 Elsa L. Gunter
Scoped Bindings
= fun fib n = #2 (fib_pair n)
= end;
val fib = fn : int -> int
- fib 7;
val it = 21 : int
![Page 29: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/29.jpg)
Copyright 2002 Elsa L. Gunter
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 -> 'aval tail = fn : 'a list -> 'a list
![Page 30: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/30.jpg)
Copyright 2002 Elsa L. Gunter
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;
![Page 31: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/31.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 32: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/32.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 33: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/33.jpg)
Copyright 2002 Elsa L. Gunter
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"
![Page 34: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/34.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 35: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/35.jpg)
Copyright 2002 Elsa L. Gunter
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;
![Page 36: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/36.jpg)
Copyright 2002 Elsa L. Gunter
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 *)
![Page 37: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/37.jpg)
Copyright 2002 Elsa L. Gunter
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
![Page 38: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/38.jpg)
Copyright 2002 Elsa L. Gunter
Side Effects and Imperative Programming
- get_count();
val it = 0 : int
- get_count();
val it = 1 : int
- get_count();
val it = 2 : int
![Page 39: Programming Language Concepts (CIS 635)](https://reader036.vdocuments.mx/reader036/viewer/2022081516/568143ee550346895db073c9/html5/thumbnails/39.jpg)
Copyright 2002 Elsa L. Gunter
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