处理 ML 中的异常
大家,我试图了解异常在 ML 中是如何工作的,但是我遇到了奇怪的错误,并且我无法弄清楚出了什么问题:
exception Factorial
fun checked_factorial n =
if n < 0 then
raise Factorial
else n;
fun factorial_driver () =
checked_factorial(~4)
handle
Factorial => print "Out of range.";
可能出了什么问题?预先感谢您的任何帮助。
everyone, I'm trying to understand how exceptions work in ML, but I have strange error, and I can't figure out what is wrong:
exception Factorial
fun checked_factorial n =
if n < 0 then
raise Factorial
else n;
fun factorial_driver () =
checked_factorial(~4)
handle
Factorial => print "Out of range.";
what may be wrong? thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保 Factorial_driver 具有一致的类型。非例外情况返回
int
,因此 ML 推断该函数的类型为unit ->; int
,但特殊情况(即print
表达式)返回unit
,而不是int
。一般来说,您基本上需要在所有情况下返回相同类型的值。
You need to make sure that
factorial_driver
has a consistent type. The non-exceptional case returnsint
, so ML infers the function to be of typeunit -> int
, but the exceptional case (that is, theprint
expression) returnsunit
, notint
.Generally, you basically need to return a value of the same type in all cases.