<< >> Up Title Contents

2.1.1.1. first/2

A predicate, which when supplied with a list a first argument, returns the first element of the list as its second argument:

first([First|Rest], First).[3]

[3] The first/2 predicate could just as well have been defined as follows, with explicit calls to the unification operator =/2:

first(List, First):-
     List = [Head|Tail],
     First = Head.


The version in the text has moved these calls from the body of the definition into the head of the definition (as a result of which the body is empty. This technique is called 'unfolding' and is a commonly used technique in Prolog programming.


<< >> Up Title Contents