处理 ML 中的异常

发布于 2024-10-08 21:12:24 字数 315 浏览 0 评论 0 原文

大家,我试图了解异常在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2024-10-15 21:12:24

您需要确保 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 returns int, so ML infers the function to be of type unit -> int, but the exceptional case (that is, the print expression) returns unit, not int.

Generally, you basically need to return a value of the same type in all cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文