324-lecture14 - Principles of Programming Languages...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Programming Languages Principles of Programming Languages Lecture 14 Lecture 14 University of Toronto Wael Aboulsaadat Wael Aboulsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://portal.utoronto.ca/ 1 ML: exceptions 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 ML: exceptions & scopes Suppose f calls g calls h, and h raises an exception: g handler is used Example: ception ; <f-expression> handle <exception i > => <exception-handler f > <g-expression> handle <exception i > => <exception-handler g > f g h raise <exception i > 4); University of Toronto- exception e1;- 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";- f(4); > 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 ML: structures ML: structures Syntax structure <structure-name> = struct (* exceptions, definitions, functions *) end structure Mapping = struct fun insert(key,value,) = [(key,value)] sert(key,value,(key1,value1) st) = University of Toronto | insert(key,value,(key1,value1) :: rest) = 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 ML: structures contd contd 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 ML: structures ML: structures contd contd Properties It is legal to define one structure within another If a structure has been defined within another structure, then its components...
View Full Document

This document was uploaded on 08/02/2011.

Page1 / 41

324-lecture14 - Principles of Programming Languages...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online