head([Y],Y):-!.
head([X|XS],X).
I understand that the head of the list is stored on the variable X.
What does the first clause mean? Is it a cut? Why?
head([Y],Y):-!.
head([X|XS],X).
I understand that the head of the list is stored on the variable X.
What does the first clause mean? Is it a cut? Why?
The first clause means if you only have one element in the list its true that (Y) will have the head. So yes it is a cut because it tells prolog to stop looking for answers if its successful up to that point. The second clause then expands on having more than one element in the list.
The implementation of head/2
is simply:
head([X | _], X).
Note that the underscore here means "zero or more elements that we don't care about".
There is no need to single out the case where the list has only one element (and then use a cut).