Lazy hanging tail in sml

I was looking through some notes, and I realized that something was wrong.

When emulating lazy calculations (without open Lazy;) for the flow from them, the following can be done.

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 to get a suspended tail, the types do not match.

operator domain: 'Z susp   
operand:         unit -> 'Y

What needs to be changed for the correct types for stail? I know what happens when the tail is not paused. I just want to find out what notes for the paused version say.

+3
source share
1 answer
fun ltail(Susp(s)) = ltail'(force s)

The problem here is that it forcetakes a type value susp, but you call it with a type value () -> 'a. That is, you take the function from the value susp, and then call forcethe function instead of the value susp. You should just do:

fun ltail s = ltail' (force s)
+2
source

All Articles