I strongly (personally) dislike use of "if" syntax, but only because of the counterintuitive "true" evaluation.<div><br></div><div>What if the language were to return the atom "else" instead of "true"?</div><div><br></div><div>I doubt that would be considered "better" but it would sure make things more readable...</div><div><br></div><div>/s<br><br>On Friday, June 14, 2013 8:06:33 AM UTC-5, Lars Herbach wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<div style="word-wrap:break-word">
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" target="_blank">http://shop.oreilly.com/<wbr>product/0636920030034.do</a></div>
</div>
</blockquote></div>