Is it possible to have lazy lists in Prolog? Something like the following:
ones([1 | Y]) :- ones(Y).
Although this obviously doesn\'t work
Yes, it's possible to have lazy lists in Prolog. Here's an infinite, lazy list of ones using freeze/2.
ones(L) :-
freeze(L, (L=[1|T],ones(T)) ).
Using it at the top-level looks like this:
?- ones(Ones), [A,B,C|_] = Ones.
A = B = C = 1.
You might also enjoy the list_util pack (for SWI-Prolog) which contains several lazy list predicates.