I have a recursive function in LISP to rotate list to right or left as following:
(If the number is positive- push to the left, if it's negative - push to the right)
> (rotate-n ‘(5 6 7 8 9) -3)
(7 8 9 5 6)
> (rotate-n ‘(5 6 7 8 9) 3)
(8 9 5 6 7)
The function:
(defun rotate-n (L n)
(cond ((= n 0) L)
((> n 0) (rotate-n (rotate-left L) (- n 1)))
((< n 0) (rotate-n (rotate-right L) (+ n 1)))))
Is there a way to get the same result using tail-recursion? The functions rotate-right and rotate-left work fine.
EDIT: I confused. The function I wrote above was tail-recursion. If I am not wrong, this function is regular recursion for the same purpose:
(defun rotate-n-r (L n)
(cond ((= n 0) L)
((> n 0) (rotate-left (rotate-n-r L (- n 1))))
((< n 0) (rotate-right (rotate-n-r L (+ n 1))))))
rotate-n