% Arguments: List of incrementing numbers, first number, length
incrementing_list([H|T], H, Len) :-
incrementing_list_(T, H, 1, Len).
incrementing_list_([], _, LenUpto, Len) :-
% Improve termination
(var(Len) -> LenUpto = Len ; LenUpto =:= Len, !).
incrementing_list_([H|T], Prev, LenUpto, Len) :-
% Prevent infinite loop: ?- incrementing_list(Lst, 3, 3).
num_lt_numvar(LenUpto, Len),
H is Prev + 1,
LenUpto1 is LenUpto + 1,
incrementing_list_(T, H, LenUpto1, Len).
num_lt_numvar(Num, NumVar) :-
(var(NumVar) -> true ; Num < NumVar).
Is general, with proper termination:
?- incrementing_list(Lst, 3, N).
Lst = [3],
N = 1 ;
Lst = [3,4],
N = 2 ;
Lst = [3,4,5],
N = 3 ;
?- incrementing_list([3, Second, Third, Last], First, Len).
Second = Len, Len = 4,
Third = 5,
Last = 6,
First = 3.
?- incrementing_list(Lst, 1, 2).
Lst = [1,2].
?- incrementing_list([3, 4, 5, 6], First, Len).
First = 3,
Len = 4.