Vlad Nicola
Vlad Nicola

Reputation: 189

Getting the first/second element of a tuple in Prolog

I haven't found how I can get the first/second element of a tuple in Prolog, I tried arg, but it only works on predicates. My list looks like this:

Pairs = [
  (pos(1, 2), pos(1, 3)),  
  (pos(1, 2), pos(2, 1)),  
  (pos(1, 2), pos(2, 3)),  
  (pos(1, 2), pos(3, 1)),  
  (pos(1, 2), pos(3, 2)),  
  (pos(1, 3), pos(2, 1)),  
  (pos(1, 3), pos(2, 3)),  
  (pos(..., ...), pos(..., ...)),  
  (..., ...)|...].

Upvotes: 3

Views: 3110

Answers (1)

fferri
fferri

Reputation: 18950

You don't need any special predicate to do that, just unification, e.g.:

?- (A, B) = (pos(1, 2), pos(4, 3)).
A = pos(1, 2),
B = pos(4, 3).

?- (pos(X1, Y1), pos(X2, Y2)) = (pos(1, 2), pos(4, 3)).
X1 = 1,
Y1 = 2,
X2 = 4,
Y2 = 3.

Unification can happen in the body of a rule, or directly in the head of the rule. Consider the following example "visiting" all pairs:

visit_pairs([]).               % same as: %  visit_pairs([]).
visit_pairs([(A, B) | Xs]) :-  %   -->    %  visit_pairs([X|Xs]) :-
    writeln(first_pair(A)),    %          %      (A, B) = X,
    writeln(second_pair(B)),   %          %      writeln(first_pair(A)),
    visit_pairs(Xs).           %          %      writeln(second_pair(B)),
                               %          %      visit_pairs(Xs).

visit_pairs will work only with lists of tuples, and each element of the list must unify with (A, B).

?- visit_pairs([(pos(1, 2), pos(1, 3)),  (pos(1, 2), pos(2, 1)),  (pos(1, 2), pos(2, 3)),  (pos(1, 2), pos(3, 1)),  (pos(1, 2), pos(3, 2)),  (pos(1, 3), pos(2, 1)),  (pos(1, 3), pos(2, 3))]).
first_pair(pos(1,2))
second_pair(pos(1,3))
first_pair(pos(1,2))
second_pair(pos(2,1))
first_pair(pos(1,2))
second_pair(pos(2,3))
first_pair(pos(1,2))
second_pair(pos(3,1))
first_pair(pos(1,2))
second_pair(pos(3,2))
first_pair(pos(1,3))
second_pair(pos(2,1))
first_pair(pos(1,3))
second_pair(pos(2,3))
true.

Another way could be to use nth0/3 to access one specific element by index, and then again using unification to unpack the tuple:

?- Pairs = [(pos(1, 2), pos(1, 3)),  ...], nth0(0, Pairs, Elem), (A, B) = Elem.
Elem =  (pos(1, 2), pos(1, 3)),
A = pos(1, 2),
B = pos(1, 3).

but this looks rather atypical.

Upvotes: 4

Related Questions