General
Material
Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5

Solution

%% This 3-place version of get_nth_element calls a four place version
%% of get_nth_element which specifies that we are at the moment
%% looking at the first element of the list.
get_nth_element(N,InList,OutElement) :-
	get_nth_element(N,1,InList,OutElement).

%% If we are supposed to get the Nth element (as specified by the
%% first argument) and we are at the moment looking at the Nth element
%% (as specified by the second argument), then we are done. We return
%% the head of the current input list.
get_nth_element(N,N,[Head|_Tail],Head).

%% If we are supposed to get the Nth element and we are currently
%% looking at the Mth element and N is still greater than M, then we
%% examine the tail (cut of the head) and specify that the we are
%% now looking at the M+1th element. In other words, the head of the
%% tail is the M+1th element of the original list.
get_nth_element(N,M,[_Head|Tail],OutElement) :-
	N > M,
	NewM is M + 1, 
	get_nth_element(N,NewM,Tail,OutElement).
    

Back to the exercise.