Yes, it worked in SWI, Ciao, and Yap (the three versions of Prolog I have available). Am able to rewrite primes/3 and twinPrimes/3 as: primes(M,N,P) :- findall(X, (between(M,N,X), is_prime(X)), P).
twinPrimes(M,N,P) :- findall([X,Y], (between(M,N,X), Y is X+2, is_prime(X), is_prime(Y)), P).
Thank you very much, Robert Buckley
On Wed, Oct 5, 2016 at 1:54 AM, Jose F. Morales <josef.morales(a)imdea.org> wrote:
On Tue, Oct 4, 2016 at 5:47 PM, D.R. Buckley, Jr. < delmas.buckley(a)gmail.com> wrote:
Downloaded and installed the "stable" version (1.14) on Ubuntu and tried to run code that works fine on SWI-Prolog. One predicate used is call/1 and Ciao places that in a library hiord_rt that doesn't appear to be included in the stable version. How do I get and install that library? Or is there a workaround? I want to use it in the following code that adds list comprehensions to Prolog.
%-------------------------------------------------------
%% list comprehension defined
:- op(700, xfx, [ <- ]). :- op(450, xfx, [ .. ]). :- op(1100, yfx, [ & ]).
Y <- M .. N :- integer(M), integer(N), M =< N, between(M, N, Y).
Z <- {Var & Dec & Pred} :- findall(Var, maplist(call, [Dec, Pred]), Z).
%-------------------------------------------------------
Dear Robert Buckley,
There is no need for combining maplist/2 and call/1. This is a more direct implementation:
Z <- {Var & Dec & Pred} :- findall(Var, (call(Dec), call(Pred)), Z).
This gives the same results as the example from https://rosettacode.org/wiki/List_comprehensions#Prolog . E.g.,
?- V <- {X, Y, Z & X <- 1..20, Y <- X..20, Z <- Y..20 & X*X+Y*Y =:= Z*Z}.
V = [(3,4,5),(5,12,13),(6,8,10),(8,15,17),(9,12,15),(12,16,20)] ?
In any case, the code above has some problems. I would not use it except for toy examples. Using "idiomatic" Prolog may result in cleaner and more efficient code. There is a relation between the Haskell list type, lazy evaluation, and Prolog backtracking. findall/3 (or setof/3, bagof/3, etc.) are useful to compute all the solutions of a predicate or goal, which is equivalent of fully evaluate a "list monad" in Haskell. In some sense Prolog does not have list comprehensions because it *already* had them before they were invented.
This code:
?- V <- {X, Y, Z & X <- 1..20, Y <- X..20, Z <- Y..20 & X*X+Y*Y =:= Z*Z}.
can be written in a more standard style (readable by other Prolog programmers) as:
?- findall((X,Y,Z), (between(1,20,X), between(X,20,Y), between(Y,20,Z), X*X+Y*Y =:= Z*Z), V).
I hope this helps!
Cheers,
-- Jose
Ciao-users mailing list Ciao-users(a)clip.dia.fi.upm.es http://cliplab.org/cgi-bin/mailman/listinfo/ciao-users