;
;; rotate list
; [July 26, 2014 / modified April 24, 2022]
;
; arg1: a list
; arg2: opt(def: -1) 1 or -1 to determine rotation direction
;
; Example
; (rotate '(1 2 3)) => (2 3 1)
; (rotate '(1 2 3) 1) => (3 1 2)
(define rotate
(lambda (lst . amt)
(if (null? amt)
(set! amt -1)
(set! amt (car amt)))
(let loop ((l (if (> amt 0) (reverse lst) lst))
(cnt (abs amt)))
(if (<= cnt 0)
(if (> amt 0) (reverse l) l)
(loop (append (cdr l) (list (car l)))
(- cnt 1))))))