prolog

How do I find all solutions to a goal in Prolog?

I have predicate P1 that returns values one after the other like this: -? P1(ARGUMENTS, RETURN). -? RETURN = 1; -? RETURN = 2; -? RETURN = 3; -? fail. I also have another predicate called P2: P2(ARGUMENTS, LIST) :- P1(ARGUMENTS, RETURN),... % SOMEHOW HERE I NEED TO INSERT ALL VALUES OF RETURN TO LIST. How do find all of the values ...

Is there an alphabetical character check in prolog?

Greetings, Is there was test or a predicate I can use in prolog to verify that a certain given character is alphabetical? Right now, what I'm doing is: List of unallawed characters: \n -> 10, space -> 32, !->33, .->46, ,->44, :->58, ;->59% % 63->? , 45 -> -, 34->", 39-> % \+memb...

What are the advantages of using Prolog over other languages?

Every language that is being used is being used for its advantages, generally. What are the advantages of Prolog? What are the general situations/ category of problems where one can use Prolog more efficiently than any other language? ...

prolog unification resolution

Why does this work: power(_,0,1) :- !. power(X,Y,Z) :- Y1 is Y - 1, power(X,Y1,Z1), Z is X * Z1. And this gives a stack overflow exception? power(_,0,1) :- !. power(X,Y,Z) :- power(X,Y - 1,Z1), Z is X * Z1. ...

How to implement tail recursion for tree algorithms in prolog.

How could I convert the following into a tail recursive version. sum(void,0). sum(t(V,L,R),S) :- sum(L,S1), sum(R,S2), S is V + S1 + S2. It seems impossible to maintain a single accumulator as the branching is in 2^n magnitude. A possible solution would be to have the accumulator add a new accumulator to a list on each iterati...

Prolog list difference routine.

I am trying to implement a list difference routine in prolog. For some reason the following fails: difference(Xs,Ys,D) :- difference(Xs,Ys,[],D). difference([],_,A,D) :- D is A, !. difference([X|Xs],Ys,A,D) :- not(member(X,Ys)), A1 is [X|A], difference(Xs,Ys,A1,D). When trying: ?- difference([1,2],[],D). I get this error: ER...

Prolog is vs = with lists.

Why does this fail L is [1,2,3,4], and this works: L = [1,2,3]? But L is 1, and L = 1 both work the same. ...

What's an elegant way to unify X,Y with (1,2), (1,-2), (-1,2), (-1,-2), (2,1), (2,-1) , (-2,1), (-2,-1)?

What's an elegant way to unify X,Y with (1,2), (1,-2), (-1,2), (-1,-2), (2,1), (2,-1) , (-2,1), (-2,-1)? Doing it this way seems error prone and tedious: foo(1,2). foo(1,-2). foo(-1,-2). ... ... ... And this way seems too expensive: foo(X,Y) :- L = [1,-1,2,-2], member(X,L), member(Y,L), abs(X,X1), abs(Y,Y1), X1 =\= Y1. ...

How to expand a resulting list in SWI-Prolog?

?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. ...

Prolog Problem with "is" function

I am having a problem with prolog that I do not understand. I have used the tracer to follow the problem, and this is what happens .... (8) 8 NEXT value(debt, p9, Orly) (8) 8 *EXIT value(debt, p9, low) (9) 8 CALL P is low S (10) 9 CALL error_handler(21, P is low, eclipse, sepia_kernel) ...error crap, more "leaves" calls.... Wh...

Binary Tree Isomorphism

I have an assignment to write among other things, a set of prolog predicates that determine if any two binary tree's are isomorphic to each other. The predicate must also be able to provide all of the isomorphic graphs to any given binary tree. Here is what I have so far, it works except for returning duplicates. % Clause: btree_iso ...

tail recursion sum, power, gcd in prolog?

hi guys, how can I accomplish this: Give a tail-recursive definition for each of the following predicates. power(X,Y,Z): XY=Z. gcd(X,Y,Z): The greatest common divisor of X and Y is Z. sum(L,Sum): Sum is the sum of the elements in L. so far I have done this but not sure if that's correct power(_,0,1) :- !. power(X,Y,Z) :- Y1 is Y -...

perfect numbers between 1 and 100

How can I accomplish writing a program to generate all perfect numbers between 1 and 100. A perfect number is a positive integer that is equal to the sum of its proper divisors. For example, 6(=1+2+3) is a perfect number. I am a beginner and am stuck on this. I just stared learning some prolog which seems very powerful but hard to catc...

Simple graph search in Prolog

Hi, I'm trying to code a simple graph search in SWI-Prolog. I came up with the following program: adjacent(1,4). adjacent(4,2). adjacent(3,6). adjacent(6,4). adjacent(7,10). adjacent(4,9). adjacent(5,10). adjacent(7,10). adjacent(7,12). adjacent(6, 11). adjacent(13,11). adjacent(9,14). adjacent(14,12). adjacent(10,15). adjacent(9,12). ...

Documentation for the Prolog dialect Prova

I would like to switch from SWI-Prolog to Prova - but it seems to be harder than expected: Predicates like succ() are not available and operations like Var1+Var2>Var3 do not work (obviously it has to be Var3<Var1+Var2 to be valid). Is there documentation available describing the differences between Prolog dialects? ...

Prolog Conditional Not Evaluating

Hi, I'm trying to simply do a conditional in prolog like this: ((Life==dead)->Trans=no). I thought the above code would evaluate as if Life == dead, then Trans = no, but for some reason its not? Thanks. ...

push a list to Stack as one element without brackets

I have list of elements in a list like [1,2,+] and I want to push them as a one element onto a stack. I can do that by putting them between square brackets but this will make brackets appear in the output. For Example, I want to push the elements of the list [1,2,+] onto a stack: stack([1,2,+],S,Y). Where stack is: stack(T,S,[T|S])...

Prolog A predicate for alignment

By alignment I mean that the predicate takes in two lists, well three with the alignment list. And then check that every item in the alignment list is indeed an element in both the other lits. And there is a requirement about order, so that rules out just checking that every item in the alignment list is a member of both the other input ...

Prolog Find the longest list in a list of lists

I have a list of lists, and I need to find the longest one of them. If there are more than one with the same length it's the same which it returns. Thanks. ...

Prolog Problem with combinding predicates that work on their own

Here we go, bear with me. The over-all goal is to return the max alignment between two lists. If there are more than one alignment with the same length it can just return the first. With alignment I mean the elements two lists share, in correct order but not necessarily in order. 1,2,3 and 1,2,9,3; here 1,2,3 would be the longest alignm...