sml懒人悬尾

发布于 2024-10-21 23:19:36 字数 630 浏览 3 评论 0原文

我正在查看一些笔记,然后我意识到有些不对劲。

当模拟延迟计算(没有 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 技术交流群。

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

发布评论

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

评论(1

苄①跕圉湢 2024-10-28 23:19:36
fun ltail(Susp(s)) = ltail'(force s)

这里的问题是 force 采用 susp 类型的值,但您使用 () 类型的值调用它 -> 'a.即,您将函数从 susp 值中取出,然后在函数上调用 force 而不是 susp 值。你应该这样做:

fun ltail s = ltail' (force s)
fun ltail(Susp(s)) = ltail'(force s)

The problem here is that force takes a value of type susp, but you call it with a value of type () -> 'a. I.e. you take the function out of the susp value and then call force on the function instead of the susp value. You should just do:

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