Prolog's =.. functionality
Using Prolog's =.., is it possible to get something such as f(x),g(开发者_StackOverflow社区x)
from [f,x,g,x]
? I can use H =.. [f,x,g,x]
to get f(x,g,x)
but this isn't what I want. Apologies if this is a silly question or I'm using =.. incorrectly or something.
Anyone know of any good websites explaining the use of =.. ?
=../2
(also called univ/2) is capable of constructing a term from a list, where the first element of the list will be the functor of the newly created term, and the other elements of the list will be its arguments. It can't create multiple terms at once. For this purpose, it has to be used as many times as the number of terms you want to create.
In your case, it seems you want three terms: f(x)
, g(x)
, and the conjunction between those two. The following goal, issued at the prompt of any Prolog top-level implementation, should work fine (here I'm using GNU Prolog because results are showed more clearly than e.g. SWI):
| ?- Fx =.. [f, x], Gx =.. [g, x], H =.. [',', Fx, Gx].
Fx = f(x)
Gx = g(x)
H = f(x),g(x)
Of course, you can define your own predicate to encapsulate this functionality if you need to reuse it in multiple places.
As far as explanations of =../2
are concerned, I learned Prolog in class and on books, so I'm not that knowledgeable when it comes to online resources. However, there is another StackOverflow question that may be of help to you.
=.. is sometimes called the univ operator, and as far as i know it operates pretty much as you say, but only for ONE functor.
?= f(x,y,z) =.. L
unifies so that
L = [f,x,y,z]
evaluates to yes. You seem to be discussing the opposite - list on the left - which is fine. But i'm still pretty sure it works only with only functor and the rest atoms or literals. I could be wrong tho, I am not a prolog expert by any means.
http://wwwcgi.rdg.ac.uk:8081/cgi-bin/cgiwrap/wsi14/poplog/prolog/ploghelp/univ
Using univ on each pertinent part of the list will yield both terms:
?- Q=[f,x,g,x],[F,X,G,X]=Q,Fx=..[F,X],Gx=..[G,X].
Q = [f, x, g, x],
F = f,
X = x,
G = g,
Fx = f(x),
Gx = g(x).
精彩评论