By : farid seifi
Date : November 22 2020, 02:59 PM

Hope this helps Altough, there's more efficient answer to this question I will post this one for those, who are begginners in Prolog like me, cause it's using very basic operations: code :
subst(T,A,V,R):T=..L,make_simple(L,L1),replace(L1,A,V,L2),to_exp(L2,R).
make_simple([],[]).
make_simple([HT],[HW]):atomic(H),make_simple(T,W),!.
make_simple([HT],[W1W2]): \+atomic(H),H=..H1,make_simple(H1,W1),make_simple(T,W2),!.
replace([],_,_,[]).
replace([AT],A,V,[VW]): replace(T,A,V,W),!.
replace([HT],A,V,[HW]): H\=A,\+is_list(H),replace(T,A,V,W),!.
replace([HT],A,V,[W1W2]):H\=A, is_list(H),replace(H,A,V,W1),replace(T,A,V,W2),!.
to_exp([],[]).
to_exp([Z,H1,H2],L):atomic(H1),atomic(H2),L=..[Z,H1,H2],!.
to_exp([Z,H1,H2],L): \+atomic(H1),atomic(H2),to_exp(H1,W1),L=..[Z,W1,H2].
to_exp([Z,H1,H2],L): atomic(H1),\+atomic(H2),to_exp(H2,W1),L=..[Z,H1,W1].
to_exp([Z,H1,H2],L): \+atomic(H1),\+atomic(H2),to_exp(H1,W1),to_exp(H2,W2),L=..[Z,W1,W2].
Share :

Compare two atoms in Prolog
By : Shirabthinath NJ
Date : March 29 2020, 07:55 AM
may help you . I have been learning prolog.. I'm using a editor named prol1.1.1 I need to write rule to compare 2 strings, when i give something like , You are trying to compare atoms, not strings. Anyways, you need \= code :
? aaa = aaa.
true.
? aaa \= aaa.
false.
? aaa \= aab.
true.

Basic Prolog  Arbitrary amount of atoms (new to Prolog)
By : Chris Jackson
Date : March 29 2020, 07:55 AM
Any of those help , If I understand your question, you must pass a list of atoms: code :
palindrome_list_atomes(Atoms):
list_concat(Atoms, Big),
atom_chars(Big,Chars),
palindrome_list(Chars).
list_concat([], '').
list_concat([AAs], R) :
list_concat(As, T),
atom_concat(A, T, R).

Count atoms in prolog
By : RanjitG
Date : March 29 2020, 07:55 AM
hope this fix your issue The main problem is that ZZ is always 1 (the only thing it ever gets bound to); which is fortunate, since you require it to be the same for both subadditions. Your second argument is just a copy of the first, and serves no purpose (at least in this problem); similarly, it isn't clear what the roles of the 3rd & 4th arguments are (3rd has the count in the base case, but 4th does for the recursive one). code :
addition(X, 1) : atom(X).
addition(X+Y, ZZ) :
addition(X, Z1),
addition(Y, Z2),
ZZ is Z1+Z2.

PrologSplitting an algebraic expression using only "+" into separate lists of atoms and numbers
By : user2218828
Date : March 29 2020, 07:55 AM
help you fix your problem I am trying to create a predicate split_exp, which takes an algebraic expression that uses only "+" functor and generates two lists, one of the atoms and one of numbers. For example, 1+a+b+3 should generate [1,3] and [a,b]. The expression can be of any length. I have tried. , Should be easy to spot from your code, corrected and simplified: code :
split_exp(X,[X],[]):number(X).
split_exp(X,[],[X]):atom(X).
split_exp(Y+X,[XNs],As):number(X),split_exp(Y,Ns,As).
split_exp(Y+X,Ns,[XAs]):atom(X),split_exp(Y,Ns,As).

Prolog  How to separate atoms from expression involving predicates?
By : G.Krenzer
Date : March 29 2020, 07:55 AM
like below fixes the issue Your predicates are pretty mixed up. For starters, you're using X \= _+_ to prevent the other rule from matching; instead you should use atomic(X). You're then saying _*_, _ which is not clearly saying anything in particular except that certain anonymous variables... exist? Anyway, the rest of the first clause is erroneous for one reason or another. Your second clause is off to a decent start, but I think you are avoiding a use of append/3 here for no particular reason. In the head, you are expecting H to be an atom, but then with the first term in the body you're forcing H to be a singleton list. What if X = a*b? You'd expect split2(a*b,[a,b]) to unify. code :
split2(X, [X]) : atomic(X).
split2(X, Result) : split2(X, Result).
split2(X+Y, Result) :
split2(X, XVars),
split2(Y, YVars),
append(XVars, YVars, Result).



Related Posts :
