spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
  Home » PROLOG » Page 1
Prolog Syntax Error : Operator expected
You need to say at the beginning of the file/module :- use_module(library(clpfd)). And apart from that, you most probably want to say T2 #>= T1 + L1. Also, abs(T2-T1) #>= min(L1,L2) can be said, regardless of the order.

Categories : Prolog

Prolog - Matrix equation
To utilize clpfd, let's use library(clpfd)), not library(bounds)! :- use_module(library(clpfd)). We can state the constraints that must hold like this: m3x3_zs(Mss,Zs) :- Mss = [[M11,M12,M13], [M21,M22,M23], [M31,M32,M33]], Zs = [ M11,M12,M13 , M21,M22,M23 , M31,M32,M33 ], Zs ins 0..9, 5 #= M11+M12-M13, % row constraints 8 #= (M21-M22)*M23, % (see t

Categories : Prolog

Somebody could help me with this little program in prolog?
(Your program has several errors: In particular use_module(xx) must be prefixed with :-) constraints/1 essentially looks like this: constraints(L) :- member(a, L), member(b, L), member(c, L), member(d, L). So you demand that a, b, c, and d are elements of L. But nowhere do you state how long the list should be. You can observe this by looking at alternate answers pressing ;Return.

Categories : Prolog

Converting input to list in prolog
In SWI-Prologyou can use atomic_list_concat/3 with the first argument (a list) uninstantiated, the second argument the separatorn (',' in your case) and the third argument the atom you already have (A). Note that this use of atomic_list_concat/3 is not portable E.g.: atomic_list_concat(L, ',', A)

Categories : Prolog

Printing on separate lines from a list
You can do it by applying writeln to list elements using maplist/2: mylist([quick,brown,fox,jumps,over,the,lazy,dog]). :-mylist(X), maplist(writeln, X). Demo. The above prints quick brown fox jumps over the lazy dog

Categories : Prolog

How to change string to integer
Prolog datatypes don't include 'strings'. SWI-prolog added it recently, but you can stay on ISO safety lane with number_codes

Categories : Prolog

Inequality solving using Prolog
library(clpr) documentation advises that it deals with non linear constraints only passively, so you're out of luck. You need a more sophisticated algebra system.

Categories : Prolog

Converting a user input string to list
SWI-Prolog offers tokenize_atom ?- [library(porter_stem)]. ?- tokenize_atom('How are you today ?', L). L = ['How', are, you, today, ?]. note that quoting atoms is necessary only when the lexical representation aliases some other type (notably variables)

Categories : Prolog

Prolog - Subset
(Many Prolog systems including SICStus and SWI have a subset/2 in their library, but rather subset(Subset, Set) ; and it is also not a clean relation...) It all depends on what you mean by a set. Is [1, 1] a valid set? Do they have to occur in one order or the other? Your definition is fine, if you permit duplicates. After all your definition reads: set_subset(Set, Subset): All elements of Su

Categories : Prolog

set up a Parent son relationship in prolog
Your parent(X, L) predicate only takes 2 variables, X and L, but you are trying to call it with 3. You need to either create or update a parent predicate to take 3 parameters, or you need to not pass Solution into parent. Without seeing the rest of the code, we can't be sure which way is correct.

Categories : Prolog

Prolog - Arguments are not sufficiently instantiated - Knight/chessboard
It's breaking on the >= test in validSquare. You can't determine the truth value of X <= Y when both are unbound, basically, because numerical operators don't assign values. You could solve this by using a member to bind the coordinates to valid squares, i.e. validSquare(X1/Y1, X2/Y2):- member(X1, [1,2,3,4,5,6,7,8]), member(X2, [1,2,3,4,5,6,7,8]), member(Y1, [1,2,3,4,5,6,7,8]),

Categories : Prolog

Represent following fact in prolog
Prolog allows to represent knowledge by facts and rules. A fact and a rule have the following format: A :- A1, .., An Where A, A1, .., An are so called literals. If n=0 then its a fact, if n>0 then its a rule. A literal has the folllowing syntax, name is the predicate name and the terms are the arguments of the predicate: literal = atom [ "(" term { "," term } ]. Knowledge respresentati

Categories : Prolog

Not in Prolog and use of Bagof
Use of not You can't use Prolog predicates like functions. not/1 is a predicate which accepts a query as an argument. So this isn't doing what you think: movie(X,not(abrams),fantasy). This is querying movie with a second argument of not(abrams). You don't have any facts or predicates that match movie(_, not(_), _) so it will always fail. If you want to know which films were not thrillers, you

Categories : Prolog

prolog implement custom flatten
Not perfect but works: myflat([[A,B|List]|ListOfLists], [A|Output]) :- myflat([[B|List]|ListOfLists], Output). myflat([[A],[A|List]|ListOfLists], [A,0|Output]) :- myflat([[A|List]|ListOfLists], Output). myflat([[A],[B|List]|ListOfLists], [A|Output]) :- A == B, myflat([[B|List]|ListOfLists], Output). myflat([[A]], [A]). Sample input/output: ?- myflat([[1,2,1],[1,2],[2,1]], X). X

Categories : Prolog

Sum of alternating elements of a list
Things like these can be better expressed with the program structure: list_altsum([], 0). list_altsum([A], S) :- S is A. list_altsum([A,B|ABs],S0) :- list_alsum(ABs, S1), S0 is A-B+S1. Alternatively: list_altsum(Xs, S) :- list_altsum(Xs, 1, S). list_altsum([], _, 0). list_altsum([A|As], F0, S0) :- F1 is -F0, list_altsum(As, F1, S1), S0 is F0*A+S1.

Categories : Prolog

prolog generate list of numbers from a list[x,y]
The problem is in your second clause. When you have [X|T], it means that T is a list. In the body you write generate([X,S1],[T]): by writing [T] you're now saying the second argument to generate is a list of which the only element is this list T. What you want to say is that it is simply this list T: generate([T,1], [T]). generate([X,S], [X|T]) :- S1 is S-1, generate([X,S1], T).

Categories : Prolog

Turn Depth First Search into a Best-First (greedy) Search in prolog
The trick is to not allow prolog to choose the next move for you, but rather gather all of the possible moves so you can use your criteria to select the best one. Look into the find_all predicate.

Categories : Prolog

Prolog simple generator
Your description is not that clear to me, I am rather guessing which values should be known and which are asked. So I will rather use library(clpfd) where I do not have to make such considerations myself. N>M>0, K>L>0, (M/N)*(K/L) = 2 and (M+K)<A p(A, M/N, K/L) :- N #> M, M #> 0, K #> L, L #> 0, M+K #< A, (M/N) * (K/L) #= 2. ?- 3/2 #= F. F = 1. ?- (

Categories : Prolog

Prolog: Splitting List into 3 Lists in Order
div(L, L1, L2, L3) :- append(L1, L1_suffix, L), append(L2, L3, L1_suffix). Do you see how this splits the three lists? Now you don't say how long you expect the lists L1, L2, and L3 to be. You can use length/2 to get the length of L and set the length of the three results if you don't want the predicate to be as general as it is at the moment. Since you say "relatively even length", whi

Categories : Prolog

How do two atomic formulas unify?
In order for unification the functors must match exactly. That is m and n are two different functors hence the entire expression cannot unify. In the first example the functors for both expressions are the same.

Categories : Prolog


Recently Add
Prolog: "false" with query
Prolog Syntax Error : Operator expected
Prolog - Matrix equation
Somebody could help me with this little program in prolog?
Converting input to list in prolog
Printing on separate lines from a list
How to change string to integer
Inequality solving using Prolog
Converting a user input string to list
Prolog - Subset
set up a Parent son relationship in prolog
Prolog - Arguments are not sufficiently instantiated - Knight/chessboard
Represent following fact in prolog
Not in Prolog and use of Bagof
prolog implement custom flatten
Sum of alternating elements of a list
prolog generate list of numbers from a list[x,y]
Turn Depth First Search into a Best-First (greedy) Search in prolog
Prolog simple generator
Prolog: Splitting List into 3 Lists in Order
How do two atomic formulas unify?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.