4Clojure-Part1

Find the odd numbers

 (fn [coll]
  (loop [coll coll odds ()]
    (if (empty? coll)
      (reverse odds)
      (recur (rest coll)
             (if (odd? (first coll))
               (cons (first coll) odds)
               odds)))))

もうちょっと短くしたいな・・・

Reverse a Sequence

(fn my-reverse [coll]
  (loop [coll coll r ()]
    (if (empty? coll)
      r
      (recur (rest coll)
             (cons (first coll) r)))))

Flatten a Sequence

(fn [coll2]
  (letfn [(my-flatten [coll]
                      (loop [coll coll f ()]
                        (if (empty? coll)
                          (sort f)
                          (recur (rest coll)
                                 (if (coll? (first coll))
                                   (concat f (my-flatten (first coll)))
                                   (cons (first coll) f)
                                   )))))]
    (my-flatten coll2))
  )

defnが使えないのでletfnを使ってみた。sortで無理やり整形してる。