Lazy lists in Prolog?

后端 未结 6 1674
情深已故
情深已故 2020-11-28 11:45

Is it possible to have lazy lists in Prolog? Something like the following:

ones([1 | Y]) :- ones(Y).

Although this obviously doesn\'t work

6条回答
  •  被撕碎了的回忆
    2020-11-28 12:43

    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.

提交回复
热议问题