Which functor should I use to express Pair and Tuples?
In the The Power of Prolog, the author says that
Pairs are terms with principal functor (-)/2.
I have some confusion because
, seems more natural.
Definitely not (,). You can use comma but it gets confusing to read because comma is used everywhere in prolog code. Most particularly, they are part of terms like something(A,B,C) and these are not pairs. So it’s convenient to write something(A, f-g, D).
(A, B) are more confusing than
I’m not sure, but I have the impression this comes from Lisp. Here we write a list as
(a b c). The Prolog equivalent is not
[a,b,c]. In addition though, Prolog has compound terms that are more convenient and efficient than lists for representing tuples. When dealing with exactly two arguments and no need for a particular name,
A-B represents an pair and library(pairs) has some useful functionality for a list of pairs. Typically though, invent a good name, e.g.,
Given a list of tuples with arbitrary arity there is still some useful reusable functionality for a list of these, such as sort/4 to sort on an arbitrary member of the tuples or
maplist(arg(3), Tuples, Result)
to get a list of all 3rd arguments from a list of tuples.
A “The Craft of Prolog” by Richard O’Keefe claims, lists should never be used of the number of elements is known and constant.