Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. ; Solving 4Clojure 44
  2.  
  3. ; first try
  4. (def rotate
  5. (fn [n xs]
  6. (if (= n 0)
  7. xs
  8. (let [nextn (* (dec (Math/abs n)) (if (< n 0) -1 1))
  9. nextxs (and (not= 0 n)
  10. (if (< n 0)
  11. (cons (last xs) (vec (butlast xs)))
  12. (conj (vec (rest xs)) (first xs))))]
  13. (if (= n 0)
  14. xs
  15. (recur nextn nextxs))))))
  16.  
  17. ; better
  18. (defn rotate-better [n xs]
  19. (let [len (count xs)
  20. n (mod n len)
  21. split (if (>= n 0) n (+ n len))]
  22. (concat (drop split xs) (take split xs))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement