haskell - Destructure a list two elements at a time (Clojure) -


this problem takes many forms. example, given input '(1 2 3 4 5 6), might want swap values between , odd pairs. output '(2 1 4 3 6 5).

in haskell, rather easy:

helper [] = [] helper [x] = [x] helper (x : y : ys) = y : x : helper ys 

i wrote clojure code accomplish same task, feel there cleaner way. suggestions on how improve this?

(defn helper [[x y & ys]]   (cond    (nil? x) (list)    (nil? y) (list x)    :else (lazy-seq (cons y (cons x (helper ys)))))) 

ideally list consumed , produced lazily. thanks.

(for [[a b] (partition 2 '(1 2 3 4 5 6))       [b a]]   i) 

or resembling haskell version:

(defn helper   ([] (list))   ([x] (list x))   ([x y & r] (concat [y x] (apply helper r))))  (apply helper '(1 2 3 4 5 6)) 

Comments

Popular posts from this blog

javascript - Unusual behaviour when drawing lots of images onto a large canvas -

how can i manage url using .htaccess in php? -

javascript - Chart.js - setting tooltip z-index -