For example, the extended euclidean algorithm (quoted from wiki):
function extended_gcd(a, b)
x := 0 lastx := 1
y := 1 lasty := 0
while b ≠ 0
quotient := a div b
(a, b) := (b, a mod b)
(x, lastx) := (lastx - quotient*x, x)
(y, lasty) := (lasty - quotient*y, y)
return (lastx, lasty)
which I tried and got:
(defn extended-gcd
[a b]
(loop [a a b b x 0 y 1 lx 1 ly 0]
(if (zero? b)
[lx ly]
(recur b (mod a b)
(- lx (* (int (/ a b)) x))
(- ly (* (int (/ a b)) y))
x y))))
I guess I could find a way to translate loops that deal with sequence. But how about this one? How do I write it in clojure way? something with map, reduce, etc. rather than loop recur.