【发布时间】:2013-08-05 11:42:30
【问题描述】:
我有以下代码:
- exception Negative of string;
> exn Negative = fn : string -> exn
- local fun fact 0 =1
| fact n = n* fact(n-1)
in
fun factorial n=
if n >= 0 then fact n
else
raise Negative "Insert a positive number!!!"
handle Negative msg => 0
end;
这有什么问题??我得到了错误:
! Toplevel input:
! handle Negative msg => 0
! ^
! Type clash: expression of type
! int
! cannot have type
! exn
我该如何解决?如果用户输入负数,我希望函数通过异常返回 0。
我也想知道当用户输入负数时如何显示消息,因为print()返回单位,但函数的其余部分返回int;
【问题讨论】:
标签: exception standards sml ml