unfold
Stephen Weeks
MLton@sourcelight.com
Tue, 17 Jul 2001 17:31:08 -0700
Henry, are these reasonable definitions of unfold and unfoldr? If so, I will
add them to my library (as well as the versions for vectors and arrays). I
agree that this is a nice abstraction.
val ('a, 'b) unfoldr: int * 'a * ('a -> 'b * 'a) -> 'b list =
fn (n, a, f) =>
if n < 0
then raise Size
else
let
fun loop (i, a, bs) =
if i >= n
then bs
else
let
val (b, a) = f a
in
loop (i + 1, a, b :: bs)
end
in
loop (0, a, [])
end
val ('a, 'b) unfold: int * 'a * ('a -> 'b * 'a) -> 'b list =
fn z => rev (unfoldr z)