A bit surprising perhaps
% works for atoms and strings ?- atom =.. X. X = [atom]. ?- "atom" =.. X. X = ["atom"]. % works for numbers ?- 3.4 =.. X. X = [3.4]. ?- 3r4 =.. X. X = [3r4]. ?- 3 =.. X. X = [3].
Overview table
This page provides a short overview of the "analysis/construction" predicates.
From O'Keefe's "The Craft of Prolog" (MIT Press, 1990), p. 145
The first is the meta-logical operations functor/3 and arg/3, particularly the latter. A word of warning here: it is almost always a bad idea to use =../2. Do not use =../2 to locate a particular argument of some term.
For example, instead of
Term =.. [_F,_,ArgTwo|_]
you should write
arg(2, Term, ArgTwo)
You will find it easier to get the explicit number "2" right than to write the correct number of "don't care" variables in the call to =../2.
Other people reading your program will find the call to arg/3 a much clearer expression of your intent. Your program will also be more efficient. Even if you need to locate several arguments of a term, it is clearer and more efficient to write
arg(1, Term, First), arg(3, Term, Third), arg(4, Term, Fourth)
than to write
Term =.. [_,First,_,Third,Fourth|_]
Do not use '=..'/2 when you know the functor. (That is, when you know both the function symbol and the arity.)
For example, one Prolog programmer actually wrote the following code:
add_date(OldItem, Date, NewItem) : - OldItem =.. [item, Type, Ship, Serial], NewItem =.. [item,Type,Ship,Serial,Date].
This could have been expressed more clearly and more efficiently as
add_date(OldItem, Date, NewItem) : - OldItem = item(Type,Ship,Serial), NewItem = item(Type,Ship,Serial,Date).
or, even better, as
add_date(item(Type,Ship,Serial),Date,item(Type,Ship,Serial,Date)).