Permalink
Browse files

Solution for P1.03

  • Loading branch information...
Lee Coomber
Lee Coomber committed Mar 18, 2012
1 parent 7dd9309 commit f1162f1fd32847d9b6326ac02029a2e27d92179f
Showing with 10 additions and 1 deletion.
  1. +7 −1 src/p1.pl
  2. +3 −0 test/p1.plt
View
@@ -5,4 +5,10 @@
% Find the last but one element of a list
last_but_one(X, [X, _]) :- !.
last_but_one(X, [_,Y|Ys]) :- last_but_one(X, [Y|Ys]).
-
+
+% Find the K'th element of a list
+element_at(X, [X|_], 1) :- !.
+element_at(X, [H|L], N) :-
+ NewN is N - 1,
+ element_at(X, L, NewN).
+
View
@@ -6,4 +6,7 @@ test(p1_01) :-
test(p1_02) :-
last_but_one(c, [a, b, c, d]).
+test(p1_03) :-
+ element_at(c, [a,b,c,d,e], 3).
+
:- end_tests(p1).

0 comments on commit f1162f1

Please sign in to comment.