Menu

#211 nth0/3 in lists is off-by-one

Performance problem
open
nobody
None
5
2013-11-28
2013-11-28
No

?- import nth0/3, nth/3 from lists.
?- nth(2, [1,2,3], Z).
Z = 2
?- nth0(2, [1,2,3], Z).
Z = 2 % Oops -- result should be 3.
(The result is correct when the first arg is unininstantiated.)

A fix:

nth0(N, L, X) :-
integer(N), !,
N >= 0,
N1 is N + 1,
'$nth1'(N1, L, X).

Discussion


Log in to post a comment.