{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

324-lecture14

# 324-lecture14 - Principles Principles of Programming...

This preview shows pages 1–9. Sign up to view the full content.

Principles of Programming Languages Lecture 14 University of Toronto Wael Aboulsaadat [email protected] http://portal.utoronto.ca/ 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ML: exceptions How to handle an exception ? Syntax <expression> handle <exception 1 > => <exception-handler 1 > | <exception 2 > => <exception-handler 2 > | …. | <exception n > => <exception-handler n > If no exceptions are raised, then return the value of <expression> If <exception i > is raised then return the value of <exception-handler i > Only the first matching exception is considered. University of Toronto Example: N! / (M! (N-M)!) - exception Negative of int; - exception TooBig of int; - fun comb (N,M) = if N < 0 then raise Negative(N) else if M < 0 then raise Negative(M) else if M > N then raise TooBig(M) else fact(N) div (fact(M) * fact(N-M)); > val comb = fn : int * int -> int - fun mycomb (N,M) = comb(N,M) handle Negative(X) => ~1 | TooBig(M) => 0; > val mycomb = fn : int * int -> int - mycomb(11,8); > val it = 165 : int - mycombt(~5,123); > val it = ~1 : int 2
ML: exceptions & scopes Suppose f calls g calls h, and h raises an exception: g handler is used Example: - exception e1; <f-expression> handle <exception i > => <exception-handler f > <g-expression> handle <exception i > => <exception-handler g > f g h …… …… raise <exception i > …… - f(4); University of Toronto - exception e2; - exception e3; - fun h(1) = raise e1 | h(2) = raise e2 | h(3) = raise e3 | h(_) = "ok"; - fun g(N) = h(N) handle e2 => "error g2" | e3 => "error g3"; - fun f(N) = g(N) handle e1 => "error f1" | e2 => "error f2"; > val it = "ok" : string - f(3); > val it = "error g3" : string - f(2); > val it = "error g2" : string - f(1); > val it = "error f1" : string - f(0); > val it = "ok" : string 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ML: structures Syntax structure <structure-name> = struct (* exceptions, definitions, functions… *) end structure Mapping = struct fun insert(key,value,[]) = [(key,value)] | insert(key,value,(key1,value1) :: rest) = University of Toronto if key = key1 then (key,value)::rest else (key1,value1) :: insert(key,value,rest); fun lookup(key,(key1,value1) :: rest) = if key = key1 then value1 else lookup(key,rest); end ; 4
ML: structures – cont’d cont’d Structure access: Using long identifier • E.g. - Mapping . insert(538,"languages",[]); > val it = [(538,"languages")] : (int * string) list - Mapping . lookup(538,[(538,"languages"),(540,"courses")]); > val it = "languages" : string University of Toronto Using open function • E.g. - open Mapping; - lookup(538,[(538,"languages"),(540,"courses")]); > val it = "languages" : string 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ML: structures – cont’d cont’d Properties It is legal to define one structure within another If a structure has been defined within another structure, then its components can be accessed by an extension of the long identifier principle (x.y.z…) A structure may be opened within another to achieve greater modularity. University of Toronto However, this may lead to name redefinition problems There is no equality defined over structures. 6
ML: signatures Syntax signature <signature-name> = sig (* definitions *) end; Example: - signature OBJ_sig = University of Toronto sig type OBJECT val grow : OBJECT -> OBJECT val shrink: OBJECT -> OBJECT end; 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ML: signatures Signatures & Structures: - signature OBJ_sig = sig type OBJECT val grow : OBJECT -> OBJECT val shrink: OBJECT -> OBJECT end; - structure INT_struct : OBJ_sig = struct University of Toronto
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 41

324-lecture14 - Principles Principles of Programming...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online