Ethiopian multiplication: Difference between revisions

Content added Content deleted
(AutoIt comes AFTER AutoHotkey :-) .)
(Clojure comes BEFORE Coldfusion :-) .)
Line 469: Line 469:


</lang>
</lang>

=={{header|Clojure}}==
<lang lisp>(defn halve [n]
(bit-shift-right n 1))

(defn twice [n] ; 'double' is taken
(bit-shift-left n 1))
(defn even [n] ; 'even?' is the standard fn
(zero? (bit-and n 1)))

(defn emult [x y]
(reduce +
(map second
(filter #(not (even (first %))) ; a.k.a. 'odd?'
(take-while #(pos? (first %))
(map vector
(iterate halve x)
(iterate twice y)))))))

(defn emult2 [x y]
(loop [a x, b y, r 0]
(if (= a 1)
(+ r b)
(if (even a)
(recur (halve a) (twice b) r)
(recur (halve a) (twice b) (+ r b))))))</lang>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
Line 580: Line 607:
...equals 578
...equals 578
</pre>
</pre>

=={{header|Clojure}}==
<lang lisp>(defn halve [n]
(bit-shift-right n 1))

(defn twice [n] ; 'double' is taken
(bit-shift-left n 1))
(defn even [n] ; 'even?' is the standard fn
(zero? (bit-and n 1)))

(defn emult [x y]
(reduce +
(map second
(filter #(not (even (first %))) ; a.k.a. 'odd?'
(take-while #(pos? (first %))
(map vector
(iterate halve x)
(iterate twice y)))))))

(defn emult2 [x y]
(loop [a x, b y, r 0]
(if (= a 1)
(+ r b)
(if (even a)
(recur (halve a) (twice b) r)
(recur (halve a) (twice b) (+ r b))))))</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==