标准机器学习语法 [英] Standard ML Syntax

查看:46
本文介绍了标准机器学习语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是标准机器学习的新手,正在尝试编写以下代码

I'm new to Standard ML and trying to write the following code

 fun whilestat test stmt1  = 
        (fn x => if (test x) then (stmt1 x;whilestat test stmt1 ) else (x) );

问题是它给了我以下错误

The issue is that it gives me the following error

w.sml:21.6-22.82 Error: right-hand-side of clause doesn't agree with function result type [circularity]
expression:  ('Z -> 'Y) -> 'Z -> 'Z
result type:  ('Z -> 'Y) -> 'Z
in declaration:
whilestat2 = (fn arg => (fn <pat> => <exp>))

uncaught exception Error
 raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
         ../compiler/TopLevel/interact/evalloop.sml:44.55
         ../compiler/TopLevel/interact/evalloop.sml:292.17-292.20

我只是想模拟一个 while 条件,如果 staement 为真,则它递归,否则返回值.

Im just trying to emaulate a while condition where if the staement is true then it recurses, else returns the value.

推荐答案

问题在于 whilestat 的返回类型.在 then 分支中,您返回一个函数,而在 else 分支中,您返回的是任意一段数据.我认为您只是在 then 分支中递归时忘记传递所有参数.

The issue lies in the return type of whilestat. In the then branch, you return a function, whereas in the else branch, you return return an arbitrary piece of data. I think you simply forgot to pass all of the arguments when you recurse in the then branch.

这是我将如何编写它(另请注意,没有必要使用 fn x => ...,我认为这会导致您的困惑).

Here's how I would write it (notice also that there's no need to use fn x => ..., which I think contributed to your confusion).

fun whilestat test stmt1 x =
  if test x
  then (stmt1 x; whilestat test stmt1 x)
  else x

将来,您可能会发现在源代码中显式注释类型很有帮助,以仔细检查您的推理.我通过尝试填写下面的 ??? 发现了您的错误:

In the future, you might find it helpful to explicitly annotate types in your source code, to double check your reasoning. I found your bug by trying to fill in the ??? below:

fun whilestat (test : 'a -> bool) (stmt1 : 'a -> unit) : ??? =
  ...

这篇关于标准机器学习语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆