Haha, right on Gary! I used the later version of the original function, but 
the earlier call to the original function. Here is what it looked like in 
the beginning. This was an example of how not to write a function that 
looks up indices by value.  The example code I posted above was the 
supposed better way. 

(defn pos [e coll]
>   (let [cmp (if (map? coll)
>               #(= (second %1) %2)
>               #(= %1 %2))]
>     (loop [s coll idx 0]
>       (when (seq s)
>         (if (cmp (first s) e)
>           (if (map? coll)
>             (first (first s))
>             idx)
>           (recur (next s) (inc idx)))))))




On Thursday, May 15, 2014 1:31:34 AM UTC+9, Gary Johnson wrote:
>
> Bridget and Guru are both right about the reason that \3 is found at 
> position 13 in the string. However, the code you typed isn't valid Clojure, 
> since the pos function expects pred to be a function of v. Thus, your pos 
> call would need to look like this:
>
> (pos #(= % \3) ":a 4 :b 1 :c 3 :d 4") => (13)  ;; note that pos returns a 
> list of all indeces matching pred
>
> Alternatively, you could have defined pos to simply perform equality 
> matching on its first argument like so:
>
> (defn pos [val coll]
>   (for [[i v] (index coll) :when (= val v)] i))
>
> (pos \3 ":a 4 :b 1 :c 3 :d 4") => (13)
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to