I came across this code, which I assume will be faster than iterating one element at a time.
nth0_det(0, [Elem|_], Elem) :- !.
nth0_det(1, [_,Elem|_], Elem) :- !.
nth0_det(2, [_,_,Elem|_], Elem) :- !.
nth0_det(3, [_,_,_,Elem|_], Elem) :- !.
nth0_det(4, [_,_,_,_,Elem|_], Elem) :- !.
nth0_det(5, [_,_,_,_,_,Elem|_], Elem) :- !.
nth0_det(N, [_,_,_,_,_,_ |Tail], Elem) :-
M is N - 6,
M >= 0,
nth0_det(M, Tail, Elem).
Is there any particular reason the unrolling stopped at 6 elements? Is it just chosen arbitrarily?
Here it was first introduced https://github.com/SWI-Prolog/swipl-devel/commit/69feecc5228b933d9800ce07e125cf889271e012