Skip to content

Commit fda57b1

Browse files
authored
Simplify solution to exercise 6.3
This change is not completely equivalent to the original code, because it returns false for the query final([],[]), whereas the original code returned true. If one takes the exercise accurately, final([].[]) should return false.
1 parent da48970 commit fda57b1

File tree

1 file changed

+1
-14
lines changed

1 file changed

+1
-14
lines changed

chapter-6/exercises.pl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,7 @@
6262
%% 3. Write a predicate final(X,List) which checks whether X is the last element
6363
%% of List.
6464

65-
%% Helper function
66-
67-
%% reverse
68-
%% base case
69-
accRev([],A,A).
70-
%% inductive case
71-
accRev([H|T],A,R) :- accRev(T,[H|A],R).
72-
%% main
73-
rev(L,R) :- accRev(L,[],R).
74-
75-
%% Actual function
76-
final([],[]).
77-
final(X,List) :-
78-
rev(List, [X|_]).
65+
final(X, List) :- append(_, [X], List).
7966

8067
%% 4. Write a predicate toptail(InList,Outlist) which says `no' if inlist is a
8168
%% list containing fewer than 2 elements, and which deletes the first and the

0 commit comments

Comments
 (0)