Skip to content

Instantly share code, notes, and snippets.

@tetsu-miyagawa
Last active November 25, 2015 14:26
Show Gist options
  • Save tetsu-miyagawa/fdb6521cf5e5970fa75c to your computer and use it in GitHub Desktop.
Save tetsu-miyagawa/fdb6521cf5e5970fa75c to your computer and use it in GitHub Desktop.
CTMCP Section 2.9 Exercise 12
declare FinallyExample Eval
fun {Eval E}
if {IsNumber E} then E
else
case E
of plus(X Y) then {Eval X} + {Eval Y}
[] times(X Y) then {Eval X} * {Eval Y}
else raise illFormedExpr(E) end
end
end
end
fun {FinallyExample E}
local R V in
try
try
V={Eval E}
R=false
raise V end
catch X then
R=true
raise X end
end
catch Y then
{Browse '*** Finally Block ***'}
if R then raise Y end
else V
end
end
end
end
try
{Browse {FinallyExample plus(plus(5 5) 10)}}
{Browse {FinallyExample times(6 11)}}
{Browse {FinallyExample minus(7 10)}}
catch illFormedExpr(E) then
{Browse '*** Ilegal expression '#E#' ***'}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment