开发者

Lazy suspended tail in sml

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 ->开发者_JS百科 '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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜