rotate   scheme


Defined in:  https://github.com/lambdamusic/extempore-extensions/blob/main/init/init_lisp.xtm

Implementation

;
;; 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))))))


Back to Index

Similar Entries