7:- module(lazy, 8 [ map/3 9 , map/4 10 , repeat/2 11 , singleton/2 12 , head/2 13 , tail/2 14 , decons/3 15 , cons/3 16 , member/2 17 , empty/1 18 ]). 19 20map(P,[X|XX],[Y|YY]) :- call(P,X,Y), freeze(YY,map(P,XX,YY)). 21map(_,[],[]). 22 23map(P,[X|XX],[Y|YY],[Z|ZZ]) :- call(P,X,Y,Z), freeze(ZZ,map(P,XX,YY,ZZ)). 24map(_,[],[],[]). 25 26repeat(X,[X|L]) :- freeze(L,repeat(X,L)). 27 28unfold(P,S1,[X|XX]) :- call(P,X,S1,S2), freeze(XX,unfold(P,S2,XX)). 29 30singleton(X,[X]). 31head([X|_],X). 32tail([_|X],X). 33decons([H|T],H,T). 34cons(H,PT,[H|T]) :- freeze(T, call(PT,T)). 35empty([])
Lazy lists
This module provides implementations of list predicates that work with lists whos tails are uninstantiated until unified. Implemented using delayed goals (freeze/2). */