Working on a predicate, rotate(L,M,N)
, where L
is a new list formed by rotating M
to the right N
times.
My approach was to just append the tail of M
to its head N
times.
rotate(L, M, N) :-
( N > 0,
rotate2(L, M, N)
; L = M
).
rotate2(L, [H|T], Ct) :-
append(T, [H], L),
Ct2 is Ct - 1,
rotate2(L, T, Ct2).
Currently, my code returns L
equal to the original M
, no matter what N
is set to.
Seems like when I'm recursing, the tail isn't properly moved to the head.
You can use
append
to split lists, andlength
to create lists:Note: this only works for N <= length(L). You can use arithmetic to fix that.
Edit for clarity This predicate is defined for List and N arguments that are not variables when the predicate is called. I inadvertently reordered the arguments from your original question, because in Prolog, the convention is that strictly input arguments should come before output arguments. So, List and N and input arguments, RotatedList is an output argument. So these are correct queries:
but this:
will go into infinite recursion after finding one answer.
When reading the SWI-Prolog documentation, look out for predicate arguments marked with a "?", as in
length
. They can be used as shown in this example.