Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Solving 4Clojure 44
- ; first try
- (def rotate
- (fn [n xs]
- (if (= n 0)
- xs
- (let [nextn (* (dec (Math/abs n)) (if (< n 0) -1 1))
- nextxs (and (not= 0 n)
- (if (< n 0)
- (cons (last xs) (vec (butlast xs)))
- (conj (vec (rest xs)) (first xs))))]
- (if (= n 0)
- xs
- (recur nextn nextxs))))))
- ; better
- (defn rotate-better [n xs]
- (let [len (count xs)
- n (mod n len)
- split (if (>= n 0) n (+ n len))]
- (concat (drop split xs) (take split xs))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement