I can define an infinite data structure - aka lazy list - like this.
let 'a lazylist = Succ of 'a * (unit -> 'a lazylist);;
(Why can't I replace unit -> 'a lazylist
with () -> 'a lazylist
?)
The way I understand lazy data structures the above definition says that a lazy list consists of a tupel of a generic element 'a
and a function unit->'a lazylist
that will compute the next element in the list when called with ()
which is of type unit.
So e.g. I could generate a list that has every even number:
let rec even_list l = match l with Succ(a,l') ->
if (a mod 2 = 0) then Succ (a, fun() -> even_list (l'())
else even_list (l'());;
The way I understand it: When fun() -> even_list (l'()))
is called with the unit argument ()
it will call even_list
with the successor of l'
by giving it unit
as an argument: l'()
But is it possible for the else even_list (l'());;
part to lead to a Stack Overflow if we give even_list a lazylist as an argument that only consists of uneven elements e.g.? Whereas in the then part of the if-statement we only generate the next element of the list when called with ()
- in the else part we would search indefinitely.