<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
Hi List,
<div>I'm currently working myself through the "Études for Erlang" book [1] and in exercise it wks's to write a recursive function, calculating the greatest common divisor for two numbers N and M. The suggested solution is a single gcd/2 function with an If
condition and recursion:</div>
<div><br>
</div>
<div><font face="Courier New">gcd(M, N) -> </font></div>
<div><font face="Courier New"> if M == N -> M;</font></div>
<div><font face="Courier New"> M > N -> gcd(M - N, N;</font></div>
<div><font face="Courier New"> true -> gcd(M, N - M)</font></div>
<div><font face="Courier New"> end.</font></div>
<div><font face="Courier"><br>
</font></div>
<div>I by myself took another way, working with multiple function clauses (did I name it right?):</div>
<div>
<div><font face="Courier New"><br>
</font></div>
<div><font face="Courier New">gcd(M, N) when M == N -></font></div>
<div><font face="Courier New"> M;</font></div>
<div><font face="Courier New">gcd(M, N) when M > N -></font></div>
<div><font face="Courier New"> gcd(M - N, N);</font></div>
<div><font face="Courier New">gcd(M, N) -></font></div>
<div><font face="Courier New"> gcd(M, N - M).</font></div>
</div>
<div><font face="Courier New"><br>
</font></div>
<div>Now I've got two questions about that: </div>
<div>1) Is my solution still recursive, since I practically call different functions?</div>
<div>2) Are there any benefits in regards of efficiancy for the first solution?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Lars.</div>
<div><br>
</div>
<div><br>
</div>
<div>[1]: <a href="http://shop.oreilly.com/product/0636920030034.do">http://shop.oreilly.com/product/0636920030034.do</a></div>
</body>
</html>