Here's another way to do it!
- The "trick" is to walk down the same list at two different speeds.
- First argument indexing keeps goals
list_first_mid_last(+,?,?,?)
deterministic.
We define list_first_mid_last/4
like this:
list_first_mid_last([E|Es],E,M,L) :-
ahead_of_mid_last([E|Es],[E|Es],M,L).
ahead_of_mid_last([],[M|_],M,M).
ahead_of_mid_last([F|Fs],Es,M,L) :-
more_ahead_of_mid_last(Fs,F,Es,M,L).
more_ahead_of_mid_last([],L,[E|_],E,L).
more_ahead_of_mid_last([F|Fs],_,Es,E,L) :-
evenmore_ahead_of_mid_last(Fs,F,Es,E,L).
evenmore_ahead_of_mid_last([],L,[E|_],E,L).
evenmore_ahead_of_mid_last([F|Fs],_,[_|Es],M,L) :-
more_ahead_of_mid_last(Fs,F,Es,M,L).
Let's run a few queries and put Prolog1 and Scheme2 results side-by-side!
% Prolog % ; Scheme
?- list_first_mid_last([1],F,M,L). % > (firstMidLast `(1))
F = M, M = L, L = 1. % (1 1 1)
%
?- list_first_mid_last([1,2],F,M,L). % > (firstMidLast `(1 2))
F = M, M = 1, L = 2. % (1 1 2)
%
?- list_first_mid_last([1,2,3],F,M,L). % > (firstMidLast `(1 2 3))
F = 1, M = 2, L = 3. % (1 2 3)
%
?- list_first_mid_last([1,2,3,4],F,M,L). % > (firstMidLast `(1 2 3 4))
F = 1, M = 2, L = 4. % (1 2 4)
%
?- list_first_mid_last([1,2,3,4,5],F,M,L). % > (firstMidLast `(1 2 3 4 5))
F = 1, M = 3, L = 5. % (1 3 5)
%
?- list_first_mid_last([1,2,3,4,5,6],F,M,L). % > (firstMidLast `(1 2 3 4 5 6))
F = 1, M = 3, L = 6. % (1 3 6)
%
?- list_first_mid_last([1,2,3,4,5,6,7],F,M,L).% > (firstMidLast `(1 2 3 4 5 6 7))
F = 1, M = 4, L = 7. % (1 4 7)
Footnote 1: Using swi-prolog version 7.3.11 (64-bit).
Footnote 2: Using the scheme interpreter SCM version 5e5 (64-bit).