Batteries.LazyList.lazy_fold_right
has what seems like a strange type signature. I have two sets of questions:
-
What’s the easiest way to use
lazy_fold_right
? What’s the easiest way to construct and deconstruct theLazy.t
things that its combining function requires? -
Why doesn’t
lazy_fold_right
have a normalfold_right
signature? Why does one have to go through extra work to compute a fold lazily? Shouldn’t that extra work be done by thefold_right
function for us?
(In the example below, I use lazy_fold_right
to compute a small sum, which is kind of silly. I actually want to use it to transform a lazy list into a different lazy list in which each element depends on the previous element.)
Example sequences:
# open Batteries;;
# let num_list = List.range 0 `To 10;;
val num_list : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
# let num_lazylist = LazyList.seq 0 ((+) 1) ((>=) 10);;
val num_lazylist : int LazyList.t = <lazy>
# LazyList.to_list num_lazylist;;
- : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
Here’s how List.fold_right
and LazyList.eager_fold_right
work:
# List.fold_right;;
- : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = <fun>
# List.fold_right (+) num_list 0;;
- : int = 55
# LazyList.eager_fold_right;;
- : ('a -> 'b -> 'b) -> 'a LazyList.t -> 'b -> 'b = <fun>
That signature is what I would expect, and the function works as expected:
# LazyList.eager_fold_right (+) num_lazylist 0;;
- : int = 55
Now for lazy folding:
# LazyList.lazy_fold_right;;
- : ('a -> 'b lazy_t -> 'b) -> 'a LazyList.t -> 'b lazy_t -> 'b lazy_t = <fun>
That signature looks odd. What’s lazy_t
? (From other investigations I know that it’s also Lazy.t
from the standard library). Let’s see what happens:
# LazyList.lazy_fold_right (+) num_lazylist 0;;
Error: This expression has type int -> int -> int but an expression was expected of type int -> 'a lazy_t -> 'a
Type int is not compatible with type 'a lazy_t
I also have a long series of failed experiments trying to pass a combining function that will accept and return lazy_t
or Lazy.t
. I can add them here if anyone wants to see what I tried, but they are a complicated mess and they all produce type or syntax errors.