Clojure higher-order functions.

(defn traverse
  "A very general function for traversing sequences"
  ([s f comb]
  (if (empty? (rest s))
    (f (first s))
    (comb (traverse (rest s) f comb) (f (first s)))))
  ([s f comb seed]
   (if (empty? s) seed
     (comb (traverse (rest s) f comb seed) (f (first s))))))

(defn mymap [f s] (traverse s f conj '()))

(mymap inc '(1 2 3))

(mymap inc [1 2 3])

(defn myreduce [comb seed s] (traverse s identity comb seed))

(myreduce + 0 '(1 2 3))

;; define filter and flatten using traverse

CSci 4651 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.