If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Saturday, October 12, 2013

raising and handling exceptions in ML

fun hd xs =
case xs of
[] => raise List.empty
| x::_ => x
exception MyUndesirableCondition
exception MyOtherException of int * int
fun mydiv (x,y) =
if y=0
then raise MyUndesirableCondition
else x div y
fun maxlist (xs,ex) =
case xs of
[] => raise ex
| x::[] => x
| x::xs' => Int.max(x, maxlist(xs',ex))
(* val y = maxlist ([], MyUndesirableCondition) *)
val z = maxlist([], MyUndesirableCondition) handle MyUndesirableCondition => 42
view raw gistfile1.sml hosted with ❤ by GitHub

No comments:

Post a Comment