

% give the first element of a list
% head([a,b,c], c).
head([H|_], H).

% Give the last element of the list
% tail([a,b,c], c).
tail([T], T).
tail([_|T],X) :- tail(T, X).
	
% Return the Nth item of a list
% index([a,b,c,d],2,c).
% index([a,b,c,d],4,X) gives false.
index([H|_], 0, H).
index([_|T], Index, Item) :-
	Iminus1 is Index -1,
	index(T, Iminus1, Item).

% Be true if the item can be found in the list
% Be false otherwise.
% search([a,b,c,d],c) -> true
% search([a,b,c,d],f) -> false
search([H|_], H).
search([_|Tail], Item) :- search(Tail, Item).

% countof a given item
% countof([a,b,c,a,a], a, 3) is true.
% countof([a,b,c,a,a], x, 0) is true.
countof([], _, 0).
countof([Item|Tail], Item, Sum) :-
	countof(Tail, Item, Subtotal),
	Sum is Subtotal + 1.
countof([Head|Tail], Item, Sum) :-
	Head \= Item, 	
	countof(Tail, Item, Sum).

