Created
October 12, 2010 12:07
-
-
Save rlorca/622075 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;idea from the book onlisp, pag 30 | |
(defn rev [nums] | |
(let [f (fn [coll res] | |
(if (empty? coll) | |
res | |
(recur (drop-last coll) (conj res (last coll)))))] | |
(f nums []))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn rotate [arr op] | |
(loop [elem arr | |
left (mod op (count arr))] | |
(if (> left 0) | |
(recur | |
(conj (drop-last elem) (last elem)) | |
(dec left)) | |
elem))) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;different approach, cycling sequence | |
(defn rotate [arr times] | |
(let [size (count arr) | |
op (mod times size)] | |
(take | |
size | |
(drop op | |
(cycle arr))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DONE: find a better way to append an element to a collection. The place where conj will add the element is implementation dependent.
(elements are added at the beginning, so function starts from last element)