我是标准ML的新手,我试图编写以下代码
fun whilestat test stmt1 =
(fn x => if (test x) then (stmt1 x;whilestat test stmt1 ) else (x) );问题是它给了我以下错误
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我只是试图削弱一个时间条件,如果主键是真的,那么它会递归,否则返回值。
发布于 2017-11-28 18:13:09
问题在于whilestat的返回类型。在then分支中,您返回一个函数,而在else分支中,您返回一个任意数据。当您在then分支中递归时,我认为您只是忘记传递所有的参数。
下面是我编写它的方式(还请注意,没有必要使用fn x => ...,我认为这是造成您混淆的原因)。
fun whilestat test stmt1 x =
if test x
then (stmt1 x; whilestat test stmt1 x)
else x在将来,您可能会发现在源代码中显式注释类型,并检查您的推理是有帮助的。我试图填写下面的???,发现了您的错误:
fun whilestat (test : 'a -> bool) (stmt1 : 'a -> unit) : ??? =
...https://stackoverflow.com/questions/47537533
复制相似问题