/* Lazy lists in Picat. From Jan Burse https://groups.google.com/forum/#!topic/comp.lang.prolog/vu0_zSd6wdU """ take(0, _, R) :- !, R = []. take(N, C, [H|R]) :- M is N-1, call(C, (H|T)), take(M, T, R). fib(A, B, (C|fib(B,C))) :- C is A+B. Credits: This is stripped down modified version of Markus Triska prost without using CLP(FD) for elements. Here is a sample run: Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.1) SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software. ?- take(5,fib(0,1),L). L = [1, 2, 3, 5, 8]. ?- take(8,fib(0,1),L). L = [1, 2, 3, 5, 8, 13, 21, 34]. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. % import cp. main => go. go => take(50,$fib(0,1),L), writeln(L), nl. take(0, _, R) ?=> R = []. take(N, C, HR) => HR = [H|R], call(C, [H|T]), take(N-1, T, R). fib(A, B, T) => T = $[C|fib(B,C)], C = A+B.