repl(_,0,[]). repl(X,N,R) :- ..., M is N-1, ...