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 - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -