sml懒人悬尾
我正在查看一些笔记,然后我意识到有些不对劲。
当模拟延迟计算(没有 open Lazy;
)时,可以对一串流执行以下操作。
datatype 'a susp = Susp of (unit -> 'a)
datatype 'a stream' = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp
fun delay (f ) = Susp(f);
fun force (Susp(f)) = f ();
val rec ones' = fn () => Cons(1, delay(ones'));
val ones = delay(ones')
fun ltail(Susp(s)) = ltail'(force s)
and ltail' (Cons(x,s)) = s
但对于悬挂尾巴来说,类型并不匹配。
operator domain: 'Z susp
operand: unit -> 'Y
为了 ltail 的正确类型需要改变什么? 我知道如果尾巴没有悬起来会发生什么。 我只是想弄清楚暂停版本的注释都说了些什么。
I was going through some notes and I realized something is amiss.
When emulating lazy computation (without open Lazy;
) one can do the following for a stream of ones.
datatype 'a susp = Susp of (unit -> 'a)
datatype 'a stream' = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp
fun delay (f ) = Susp(f);
fun force (Susp(f)) = f ();
val rec ones' = fn () => Cons(1, delay(ones'));
val ones = delay(ones')
fun ltail(Susp(s)) = ltail'(force s)
and ltail' (Cons(x,s)) = s
But for getting a suspended tail the types do not match up.
operator domain: 'Z susp
operand: unit -> 'Y
What will need to change for the proper types for ltail ?
I know what happens with a tail not suspended.
I just want to figure out what the notes were saying for the suspended version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是
force
采用susp
类型的值,但您使用() 类型的值调用它 -> 'a
.即,您将函数从susp
值中取出,然后在函数上调用force
而不是susp
值。你应该这样做:The problem here is that
force
takes a value of typesusp
, but you call it with a value of type() -> 'a
. I.e. you take the function out of thesusp
value and then callforce
on the function instead of thesusp
value. You should just do: