Batteries.LazyList
allows one to define lazy lists. I would like to define a lazy list consisting of x
, f x
, f (f x)
, f (f (f x))
, etc.
Based on comments in the module documentation, it appears that from_loop
is the function I want:
"from_loop data next
creates a (possibly infinite) lazy list from the successive results of applying next
to data
, then to the result, etc."
This description suggests that if I wanted a lazy list of non-negative integers, for example, I could define it like this:
let nat_nums = from_loop 0 (fun n -> n + 1)
However, this fails because the signature of from_loop
is
'b -> ('b -> 'a * 'b) -> 'a LazyList.t
so the next
function has signature ('b -> 'a * 'b)
. In utop, the error message underlines n + 1
and says
Error: This expression has type int but an expression was expected of type 'a * int
I don't understand what 'a
is supposed to be. Why is the next
function supposed to return a pair? Why is the type of the list supposed to be a 'a LazyList.t
? Shouldn't the type of the elements be the same as the type of the argument to the next
function? The description of the function doesn't make the answers clear to me.
In case it's helpful, my conception of what I'm trying to do comes from Clojure's iterate
. In Clojure I could create the above definition like this:
(def nat-nums (iterate (fn [n] (+ n 1)) 0))