The code you pasted doesn't actually find the common divisor of two numbers.
user> (cds 100 200)
[0 100]
[0 100]
Here's my code using loop/recur
(defn cds [x y]
(loop [i (->> (min x y) inc (range 1))
ret []]
(if-let [n (first i)]
(if-not (= 0 (rem x n) (rem y n))
(recur (rest i) ret)
(recur (rest i) (conj ret n)))
ret)))
(loop [i (->> (min x y) inc (range 1))
ret []]
(if-let [n (first i)]
(if-not (= 0 (rem x n) (rem y n))
(recur (rest i) ret)
(recur (rest i) (conj ret n)))
ret)))
Here's the output
user> (cds 100 200)
[1 2 4 5 10 20 25 50 100]
[1 2 4 5 10 20 25 50 100]
You can check with WolframAlpha => https://www.wolframalpha.com/input/?i=common%20divisors%20of%20100%20200
25.10.2013, 01:18, "Bartosz Kaliszuk" <bartosz.kalis...@gmail.com>:
--On Thu, Oct 24, 2013 at 5:32 PM, Kelker Ryan <theinter...@yandex.com> wrote:Try this.
(defn my-filter [pred? coll]
(loop [c coll
ret (empty c)]
(if-let [x (first c)]
(if (pred? x)
(recur (rest c) (lazy-cat ret [x]))
(recur (rest c) ret))
ret)))While we are on loop/recur and if-let: How would if-let version of my function common divisors look like? Would I benefit from it in any way?
(defn cds [x y]
(loop [a x
b y
result []]
(if (= 0 (rem a b))
(conj result b)
(recur b (rem a b) (conj result (int (/ a b)))))))--
Regards
Bartosz Kaliszuk
bartosz(dot)kaliszuk(at)gmail(dot)com--
--
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/groups/opt_out.
--
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/groups/opt_out.