<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeMocha &#187; ml</title>
	<atom:link href="http://codemocha.com/tag/ml/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemocha.com</link>
	<description>Freshly ground code.  Thoughts on software and algorithms by Chet Mancini</description>
	<lastBuildDate>Fri, 18 Sep 2009 15:22:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Euclidean Algorithm</title>
		<link>http://codemocha.com/2009/07/euclidean-algorithm/</link>
		<comments>http://codemocha.com/2009/07/euclidean-algorithm/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:46:41 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[ml]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=9</guid>
		<description><![CDATA[One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers.  In ML or another functional language it really is beautiful:
12fun gcd&#40;a, 0&#41; = a
&#160; &#160;&#124; gcd&#40;a, b&#41; = gcd&#40;b, a mod b&#41;;
Of course, the same can be done in Java iteratively:
123456789public int gcd&#40;int a, [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers.  In ML or another functional language it really is beautiful:<span id="more-9"></span></p>
<div class="codecolorer-container ocaml vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br /></div></td><td><div class="ocaml codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">fun</span> gcd<span class="br0">&#40;</span>a, <span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">=</span> a<br />
&nbsp; &nbsp;<span class="sy0">|</span> gcd<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span> <span class="sy0">=</span> gcd<span class="br0">&#40;</span>b, a <span class="kw1">mod</span> b<span class="br0">&#41;</span><span class="sy0">;</span></div></td></tr></tbody></table></div>
<p>Of course, the same can be done in Java iteratively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">public</span> <span class="kw4">int</span> gcd<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> c<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>b<span class="sy0">&gt;</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c <span class="sy0">=</span> a<span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a <span class="sy0">=</span> b<span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span class="sy0">=</span> c<span class="sy0">%</span>b<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a<span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Or recursively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">public</span> <span class="kw4">int</span> gcd<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>b<span class="sy0">==</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> a<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> gcd<span class="br0">&#40;</span>b, a<span class="sy0">%</span>b<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/euclidean-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursive Fibonacci</title>
		<link>http://codemocha.com/2009/07/recursive-fibonacci/</link>
		<comments>http://codemocha.com/2009/07/recursive-fibonacci/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:40:28 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[college]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[ml]]></category>
		<category><![CDATA[recusion]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=7</guid>
		<description><![CDATA[Here are two recursive fibonacci functions in ML I did for CS243.  Can you tell which one is more efficient?
123456789101112fun fibonacci&#40;n&#41; = 
&#160; let
&#160; &#160; &#160; fun fib&#40;&#40;a, b&#41;, i&#41;= &#160;
&#160; &#160; if i=0 then b
&#160; &#160; &#160; &#160; else fib&#40;&#40;b, a + b&#41;, i-1&#41;;
&#160; &#160; &#160; &#160; &#160; &#160;in
&#160; fib&#40;&#40;0, 1&#41;, n&#41;
&#160; end;

fun [...]]]></description>
			<content:encoded><![CDATA[<p>Here are two recursive fibonacci functions in ML I did for CS243.  Can you tell which one is more efficient?<span id="more-7"></span></p>
<div class="codecolorer-container ocaml vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ocaml codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">fun</span> fibonacci<span class="br0">&#40;</span>n<span class="br0">&#41;</span> <span class="sy0">=</span> <br />
&nbsp; <span class="kw1">let</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">fun</span> fib<span class="br0">&#40;</span><span class="br0">&#40;</span>a, b<span class="br0">&#41;</span>, i<span class="br0">&#41;</span><span class="sy0">=</span> &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">if</span> i<span class="sy0">=</span><span class="nu0">0</span> <span class="kw1">then</span> b<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> fib<span class="br0">&#40;</span><span class="br0">&#40;</span>b, a <span class="sy0">+</span> b<span class="br0">&#41;</span>, i<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">in</span><br />
&nbsp; fib<span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span>, n<span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">end</span><span class="sy0">;</span><br />
<br />
<span class="kw1">fun</span> fibonacci<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="nu0">1</span><br />
&nbsp; <span class="sy0">|</span> fibonacci<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="nu0">1</span><br />
&nbsp; <span class="sy0">|</span> fibonacci<span class="br0">&#40;</span>n<span class="br0">&#41;</span> <span class="sy0">=</span> fibonacci<span class="br0">&#40;</span>n<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="sy0">+</span> fibonacci<span class="br0">&#40;</span>n<span class="sy0">-</span><span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/recursive-fibonacci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
