I'm trying to make a relation in Prolog shiftL(L1,N,L2)
that shifts L1
to the left N
times (rotationally), and the result is L2
, so for example shiftL([1,2,3], 2, [3,1,2])
is true.
I tried the following:
shiftL([],N,[]).
shiftL([X|Xs],1,L) :- append(Xs,[X],L).
shiftL([X|Xs],N,L) :- N1 is N-1 , N=\=1 , shiftL(L1,N1,L) , append(Xs,[X],L1).
And it works great, but after giving me the result it always keeps doing something else and I get a stack overflow:
?- shiftL([1,2,3], 2, L).
L = [3, 1, 2] ;
ERROR: Out of global stack
And I have no idea what's causing that. I thought I covered the base case with the second line, and with the N=\=1
statement.
Thanks in advance for any help!