0

I have a structure to define lazily evaluated sequences which are defined as

datatype 'a seq = Cons of 'a option * (unit -> 'a seq)

and I have a function map() that maps a function of type ('a->'b) to the values in the sequence.

val map : ('a -> 'b) -> 'a seq -> 'b seq

However when I define the function, it doesn't match the type provided by the signature, and I cannot for the life of me figure out how to make it match. Everything I tried either didn't affect it or made it even worse.

fun map fu (Cons(NONE, f)) = map fu (f())
  | map fu (Cons(SOME x, f)) = Cons(fu(x), fn () => map(fu) (f()))

Which gives me the error type in structure doesn't match signature

  spec:   ('a -> 'b) -> 'a ?.Seq.seq -> 'b ?.Seq.seq
  actual: ('a -> 'b option) -> 'a ?.Seq.seq -> 'b ?.Seq.seq
jasimp
  • 73
  • 5

1 Answers1

0

One AH moment later and I think I solved the issue. I finally realized that it was being typed that way because Cons took an option, and for some reason despite looking at it so many times I didn't even think of that. Here's my solution

fun map fu (Cons(NONE, f)) = map fu (f())
  | map fu (Cons(SOME x, f)) = Cons(SOME(fu(x)), fn () => map(fu) (f()))
jasimp
  • 73
  • 5