Prolog Language Reasoning about data Accessing lists


Example

Member

member/2 has signature member(?Elem, ?List) and denotes true if Elem is a member of List. This predicate can be used to access variables in a list, where different solutions are retrieved through backtracking.

Example queries:

?- member(X, [1,2,3]).
X = 1 ;
X = 2 ;
X = 3.

?- member(X,[Y]).
X = Y.

?- member(X,Y).
Y = [X|_G969] ;
Y = [_G968, X|_G972] ;
Y = [_G968, _G971, X|_G975] ;
Y = [_G968, _G971, _G974, X|_G978]
...

Pattern matching

When the indices you need to access are small, pattern matching can be a good solution, e.g.:

third([_,_,X|_], X).
fourth([_,_,_,X|_], X).