I need to initialize some lists with a finite number of elements, all the same.
fill( 0, [], _ ) :- !.
fill( Index, [Value|List], Value ) :-
NextIndex is Index - 1,
fill( NextIndex, List, Value ).
The cut is mandatory to avoid infinite recursion and a fail result.
I tried to modify the rule #2 but after giving the expected solution, goal fails :
fill( Index, [Value|List], Value ) :-
Index > 0,
NextIndex is Index - 1,
fill( NextIndex, List, Value ).
How it works:
?- fill( 7, AllFalse, false ).
AllFalse = [false, false, false, false, false, false, false].
?-