Darge Elar
Darge Elar

Reputation: 105

How to read brackets literally (as a tuple) in Prolog?

When I type this have this code:

?- Tuple = ((2, 4), (2, 4)).

It says:

Tuple = ((2, 4), 2, 4).

But I would want it to say:

Tuple = ((2, 4), (2, 4)).

How can I achieve that?

Upvotes: 2

Views: 220

Answers (1)

jschimpf
jschimpf

Reputation: 5034

It is generally not a good idea to think of the comma-operator as making "tuples". Pairs (as in your example) are still ok, but as you noticed, the syntax can be confusing because it hides the structure: (1,(2,3)) is the same as (1,2,3) and the same as 1,2,3.

Once you try making triplets you get into real trouble, because (1,2,3) is indistinguishable from the nested pairs (1,(2,3)). That's of course because it really is a nested pair, not a flat structure.

It is better style to use a specific functor, let's say t/N. Then there is no ambiguity between t(1,2,3) and t(1,t(2,3)).

Upvotes: 3

Related Questions