Ethiopian multiplication: Difference between revisions

Content added Content deleted
(CL Ethiopian Multiply)
Line 197: Line 197:
return 0;
return 0;
}</lang>
}</lang>

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

Common Lisp already has <code>evenp</code>, but all three of <code>halve</code>, <code>double</code>, and <code>even-p</code> are locally defined within <code>ethiopian-multiple</code>. (Note that the termination condition is <code>(zerop l)</code> because we terminate 'after' the iteration wherein the left column contains 1, and <code>(halve 1)</code> is 0.)

<lang lisp>(defun ethiopian-multiply (l r)
(flet ((halve (n) (floor n 2))
(double (n) (* n 2))
(even-p (n) (zerop (mod n 2))))
(do ((product 0 (if (even-p l) product (+ product r)))
(l l (halve l))
(r r (double r)))
((zerop l) product))))</lang>


=={{header|E}}==
=={{header|E}}==