logo
down
shadow

prolog, changing atoms in expression


prolog, changing atoms in expression

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([H|T],[H|W]):-atomic(H),make_simple(T,W),!.
make_simple([H|T],[W1|W2]):- \+atomic(H),H=..H1,make_simple(H1,W1),make_simple(T,W2),!.

replace([],_,_,[]).
replace([A|T],A,V,[V|W]):- replace(T,A,V,W),!.
replace([H|T],A,V,[H|W]):- H\=A,\+is_list(H),replace(T,A,V,W),!.
replace([H|T],A,V,[W1|W2]):-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 : facebook icon twitter icon
Compare two atoms in Prolog

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)

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([A|As], R) :-
  list_concat(As, T),
  atom_concat(A, T, R).
Count atoms in prolog

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 sub-additions.
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.
Prolog-Splitting an algebraic expression using only "+" into separate lists of atoms and numbers

Prolog-Splitting 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,[X|Ns],As):-number(X),split_exp(Y,Ns,As).
split_exp(Y+X,Ns,[X|As]):-atom(X),split_exp(Y,Ns,As).
Prolog - How to separate atoms from expression involving predicates?

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 Related Posts :
  • Magic Hexagon can't constrain itself
  • Comparing lists with custom conditions
  • Create associative list with variables as values in common lisp
  • Find all possible pairs
  • Unique elements in matrix
  • string to list of lists of rhyming words
  • Largest prefix list of lists
  • How to know when a list is ordered in CLIPS
  • Can't i remove element list during foreach loop
  • Checking the heads of lists of lists
  • Get partial results of map function
  • Get Files from Subdirectory Only
  • Prolog Choose 3 From List
  • Prolog length to initialize list adds new List to existing Lists
  • Python 2.7. Iterating list not working
  • Prolog Split list
  • Generating permutations of a list in NetLogo
  • Prolog - Check number of occurences doesn't work as expected
  • How to make the output of this prolog program state "true"?
  • wshShell and WMIC working together in batch?
  • Returning first k elements of a list. My code returns last k elements
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co