What kind of an answer are you looking for? Just the quickest way to
do it? Do you want an "idiomatic" clojure solution or are you learning
functional programming using clojure? There are many ways to do this.
Others have provided cool solutions.

Here's a way of doing it if you are interested in filled-with-fun bare-
bones no-frills functional code. It will NOT be the fastest solution.


(defn min-b [vctr collection-function]
  (let [current-item (first vctr)]
    (cond (empty? vctr) (collection-function [] -1)
          (= 1 (count vctr)) (collection-function current-item 1)
          :else (min-b (rest vctr) (fn [next-item pos] (if (< (second next-
item) (second current-item))
                                                             
(collection-function next-item (inc pos))
                                                             
(collection-function current-item pos)))))))

The initial function you pass in will contain the logic to print out
the result. Over here we are making a simple list out of the result .
You can modify it to print out whatever you want done with the result
(min-b [ [22 5] [56 8] [99 3] [43 76] ]  (fn [x y] (list x y)))
or
(min-b [ [22 5] [56 8] [99 3] [43 76] ]  (fn [x y] (println (str
"Value: " x " Position: " y))))


If you have a large data set and are worried about blowing the stack,
you'll have to use recur and trampoline. Notice the extra # before the
returned functions

(defn min-b [vctr collection-function]
  (let [current-item (first vctr)]
    (cond (empty? vctr) (collection-function [] -1)
          (= 1 (count vctr)) #(collection-function current-item 1)
          :else (recur (rest vctr) (fn [next-item pos] (if (< (second next-
item) (second current-item))
                                                             
#(collection-function next-item (inc pos))
                                                             
#(collection-function current-item pos)))))))

(trampoline (min-b (vec (take 5000 (cycle [ [22 5] [56 8] [99 3] [43
76] ])))  (fn [x y] (list x y))))


This example should demonstrate why hard tail calls are not a mere
recursion thing.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to