Hi
I am exploring lazy constructs in Prolog (and coroutines). I found out about lazy_lists and freeze and I was wondering what is the sense of the community about them.
Here is some code I wrote following the usual patterns seen in functional languages.
fibonacci_Sequence(FS) :-
lazy_list(fib_seq, state(-, -), FS).
fib_seq(state(-, -), state(0, -), 0) :- !.
fib_seq(state(0, -), state(1, 0), 1) :- !.
fib_seq(state(F2, F1), state(F, F2), F) :-
F is F1 + F2.
fib(N, F) :-
fibonacci_sequence(FS),
nth1(N, FS, F).
… and using freeze.
fibonacci_seqf([1, 1 | FS]) :-
fib_seqf(1, 1, FS).
fib_seqf(1, 1, F2) :-
freeze(F2, fib_seqf(1, 2, F2)), !.
fib_seqf(N2, N1, [N | FNS]) :-
N is N2 + N1,
freeze(FNS, fib_seqf(N1, N, FNS)), !.
fibf(N, F) :-
fibonacci_seqf(FS),
nth0(N, FS, F).
I believe the freeze versions are clearer, but that’s just me.
Apart from that, I was trying to also render in Prolog the following Haskell Fibonacci’s sequence, but I cannot get my head wrapped around it. Any suggestion will be appreciated.
fib = 1 : 1 : zipWith (+) fib (tail fib)
Thank you
all the best
MA
PS I can find the documentation for lazy_lists and freeze using search engines, but I cannot find an obvious entry in the Documentation menu on the web site.