<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The only wrong way</title>
	<atom:link href="http://ellerain.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ellerain.wordpress.com</link>
	<description>Name That Fruit blog. No, really...</description>
	<lastBuildDate>Sun, 22 Aug 2010 08:09:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ellerain.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4ae06dc437fa002e75e2dbc6c976496e?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>The only wrong way</title>
		<link>http://ellerain.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ellerain.wordpress.com/osd.xml" title="The only wrong way" />
	<atom:link rel='hub' href='http://ellerain.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SICP Exercise 1.13</title>
		<link>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12-2/</link>
		<comments>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12-2/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 18:22:00 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.2]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12-2/</guid>
		<description><![CDATA[A non-programming exercise. I have zero idea why it is here.&#160;Anyhow.&#160; SICP Exercise 1.13 fi = (1 + sqrt(5))/2 psi = (1 &#8211; sqrt(5))/2&#160; &#160; Show that (1) fib(n) = (fi^n &#8211; psi^n)/sqrt(5) &#160; For n = 1 and n = 2 simple calculation will show that (1) holds.&#160; &#160; Then let&#8217;s prove: (2)&#160;(fi^(n-1) &#8211; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=102&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;line-height:17px;margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;">A non-programming exercise. I have zero idea why it is here.&nbsp;<br style="margin:0;padding:0;" />Anyhow.&nbsp;</span></p>
<p style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;line-height:17px;margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;"><strong>SICP Exercise 1.13</strong></span></p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;">fi = (1 + sqrt(5))/2</p>
<p style="margin:0;">psi = (1 &#8211; sqrt(5))/2&nbsp;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">Show that (1) fib(n) = (fi^n &#8211; psi^n)/sqrt(5)</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">For n = 1 and n = 2 simple calculation will show that (1) holds.&nbsp;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">Then let&#8217;s prove: (2)&nbsp;(fi^(n-1) &#8211; psi^(n-1))/sqrt(5) +&nbsp;(fi^n &#8211; psi^n)/sqrt(5) =&nbsp;(fi^(n+1) &#8211; psi^(n+1))/sqrt(5)</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">(fi^(n-1) &#8211; psi^(n-1))/sqrt(5) +&nbsp;(fi^n &#8211; psi^n)/sqrt(5) = [regrouping and using commutation]</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">(fi^(n-1) * (1+fi) &#8211; psi^(n-1) (1+psi))/sqrt(5) = [since 1+fi = fi^2 and 1+psi = psi^2]</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">(fi^(n-1) * (fi^2) &#8211; psi^(n-1) (psi^2))/sqrt(5) =&nbsp;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">(fi^(n+1) &#8211; psi^(n+1))/sqrt(5)</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">QED.</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;"><span style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:small;"><span style="font-size:12px;line-height:17px;">Even after completing it I still have no idea what is this doing in SICP.&nbsp;</span></span></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=102&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.12</title>
		<link>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12/</link>
		<comments>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 17:22:47 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.2]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12/</guid>
		<description><![CDATA[And more of the same. For the fun, I coded calculations for both the rotated and normal placement of rows and columns. It&#8217;s really the typing and hand-stretching exercise.Anyhow.&#160; SICP Exercise 1.12 &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160;open System &#160;&#160;&#160;&#160;4&#160; &#160;&#160;&#160;&#160;5&#160;module Ex1d12 = &#160;&#160;&#160;&#160;6&#160;&#160;&#160;&#160; let rec pascal_triangle_element x y = &#160;&#160;&#160;&#160;7&#160;&#160;&#160;&#160; &#160;&#160;&#160; if (x &#60; 0) &#38;&#38; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=101&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;line-height:17px;margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;">And more of the same. For the fun, I coded calculations for both the rotated and normal placement of rows and columns. It&#8217;s really the typing and hand-stretching exercise.<br style="margin:0;padding:0;" />Anyhow.&nbsp;</span></p>
<p style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;line-height:17px;margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;"><strong>SICP Exercise 1.12</strong></span></p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;<span style="color:blue;">module</span> Ex1d12 =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> pascal_triangle_element x y =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (x &lt; 0) &amp;&amp; (y &lt; 0)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> 0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">elif</span> (x = 0) || (y = 0)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> 1</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pascal_triangle_element (x-1) y + pascal_triangle_element x (y-1)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> pascal_triangle row place =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (row &lt; 0) || (place&lt;0) || (place &gt; row)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> 0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pascal_triangle_element (row-place) (place)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pascal_triangle 2 1</p>
</div>
<p style="color:#555555;font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;line-height:17px;margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;"><br />
This stuff is really addictive.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/101/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=101&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.11</title>
		<link>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-11/</link>
		<comments>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-11/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 16:51:24 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.2]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-11/</guid>
		<description><![CDATA[This&#160;exercise&#160;is only fascinating if you try to nail it completely inside your head, without writing the transformations down on the piece of paper. It almost requires not-posting the solution to avoid spoiling the fun. On the other hand, I could&#8217;ve gotten it wrong. Anyhow.&#160; SICP Exercise 1.11 &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160;open System &#160;&#160;&#160;&#160;4&#160;module Ex1d11 = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=98&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;">This&nbsp;exercise&nbsp;is only fascinating if you try to nail it completely inside your head, without writing the transformations down on the piece of paper. It almost requires not-posting the solution to avoid spoiling the fun. On the other hand, I could&#8217;ve gotten it wrong.<br />
Anyhow.&nbsp;</span></p>
<p style="margin:0 0 10px;padding:0;"><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;margin:0;padding:0;"><strong>SICP Exercise 1.11</strong></span></p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d11 =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> triple_fib_iter_inner accumulator prev before_prev countdown =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> countdown = 3</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> accumulator</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> triple_fib_iter_inner (accumulator+prev+before_prev) (accumulator) (prev) (countdown &#8211; 1)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> triple_fib_iter n =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> n&lt;4&nbsp; <span style="color:blue;">then</span> n</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> triple_fib_iter_inner 3 2 1 n</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> triple_fib_rec n =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> n&lt;4 <span style="color:blue;">then</span> n</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="white-space:pre;"> </span>&nbsp;&nbsp; &nbsp;&nbsp;<span style="color:blue;">else</span> (triple_fib_rec (n-1) + triple_fib_rec (n-2) + triple_fib_rec (n-3))</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; triple_fib_iter 7 &#8211; triple_fib_rec 7</p>
<p style="margin:0;">&nbsp;</p>
</div>
<p>I had a small problem here when I accidentally wrote <code>(countdown -1)</code> instead of <code>(countdown - 1)</code>. That&#8217;s five minutes of my life I won&#8217;t get back.</p>
<p> Also, this is an example of a recurrence relation. It always is fascinating to quickly <a href="http://en.wikipedia.org/wiki/Recurrence_relation#Linear_homogeneous_recurrence_relations_with_constant_coefficients"> read about such things on Wikipedia</a>; great stuff to talk to other people at parties about is to be discovered there. Did <strong>you</strong> know that linear recursive sequences are precisely the sequences whose generating function is a rational function?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/98/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/98/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=98&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.10</title>
		<link>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-10/</link>
		<comments>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-10/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 11:00:00 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.2]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-8-2/</guid>
		<description><![CDATA[SICP seems to give some strange definition for the Ackermann function; otherwise it&#8217;s really calculus, not the Comp Sci stuff. Anyhow: SICP Exercise 1.10 &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160;open System &#160;&#160;&#160;&#160;4&#160;module Ex1d10 = &#160;&#160;&#160;&#160;5&#160;&#160;&#160;&#160; let rec A x y = &#160;&#160;&#160;&#160;6&#160;&#160;&#160;&#160; &#160;&#160;&#160; match y with &#160;&#160;&#160;&#160;7&#160;&#160;&#160;&#160; &#160;&#160;&#160; &#124;&#160;&#160; 0 -&#62; 0 &#160;&#160;&#160;&#160;8&#160;&#160;&#160;&#160; &#160;&#160;&#160; &#124;&#160;&#160; 1 -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=83&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>SICP seems to give some strange definition for the Ackermann function; otherwise it&#8217;s really calculus, not the Comp Sci stuff.<br />
Anyhow:</p>
<p><span style="font-family:Verdana, 'BitStream vera Sans', Helvetica, sans-serif;font-size:12px;color:#555555;line-height:17px;"><strong>SICP Exercise 1.10</strong></span></p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d10 =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> A x y =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">match</span> y <span style="color:blue;">with</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 0 <span style="color:blue;">-&gt;</span> 0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 1 <span style="color:blue;">-&gt;</span> 2</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; _ <span style="color:blue;">-&gt;</span> <span style="color:blue;">if</span> x = 0 <span style="color:blue;">then</span> 2*y <span style="color:blue;">else</span> A (x-1) (A x (y-1))</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&#8220;A 1 10 = %d&#8221;</span> (A 1 10)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&#8220;A 2 4 = %d&#8221;</span> (A 2 4)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&#8220;A 3 3 = %d&#8221;</span> (A 3 3)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore Console.ReadKey</p>
</div>
<p>As for the second part:<br />
A 0 n === 2*n<br />
A 1 n === 2^n<br />
A 2 n === 2^(2^(2^&#8230;(2^2))&#8230;) n times &#8211; it&#8217;s called Tetration</p>
<p>(- It has a name?<br />
- Yes, little girl. Operations do traditionally have names in most cultures.)[<a href="http://www.youtube.com/watch?v=Okkk1yMYJpg#">1:40</a>]</p>
<p>And some additional reading:</p>
<ul>
<li><a href="http://mathworld.wolfram.com/AckermannFunction.html">Ackermann Function</a></li>
<li><a href="http://en.wikipedia.org/wiki/Tetration">Tetration</a></li>
</ul>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=83&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.9</title>
		<link>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-9/</link>
		<comments>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-9/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 10:54:13 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.2]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-8/</guid>
		<description><![CDATA[Tail recursion and iterative versus recursive processing. The main idea behind tail recursion is to generate iterative process from recursive function by rewriting your code in a certain way. Although every recursive process can be converted into iterative (see&#160;Church&#8211;Turing&#160;thesis), tail-recursion can only be applied to a certain class of functions.&#160; Anyhow. SICP Exercise 1.9 A [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=76&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Tail recursion and iterative versus recursive processing. The main idea behind tail recursion is to generate iterative process from recursive function by rewriting your code in a certain way. Although every recursive process can be converted into iterative (see&nbsp;<a href="http://en.wikipedia.org/wiki/Church%E2%80%93Turing_thesis">Church&ndash;Turing&nbsp;thesis</a>), tail-recursion can only be applied to a certain class of functions.&nbsp;<br />
<em> Anyhow.</em></p>
<p><span style="border-collapse:separate;color:#000000;font-family:Arial;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:auto;text-indent:0;text-transform:none;white-space:normal;widows:2;word-spacing:0;font-size:14pt;"><strong>SICP Exercise 1.9<br />
 </strong></span></p>
<p>A bit of complication here is caused by the fact that&nbsp;<code>incr</code> and <code>decr</code> in F# are in-place and are defined as <code>int ref -&gt; uint</code>. Bugger. Attempting to use these means getting familiar (more like acquainted) with a concept of F# reference cells. Fortunately, there are only three things about those we need to know right now:</p>
<ul>
<li><span style="font-family:'Segoe UI', Verdana, Arial;font-size:13px;">You use the&nbsp;<span class="input"><code>ref</code></span>&nbsp;operator before a value to create a new reference cell that encapsulates the value.&nbsp;</span></li>
<li><span style="font-family:'Segoe UI', Verdana, Arial;font-size:13px;">When you create a reference cell by using the&nbsp;<span class="input"><code>ref</code></span>&nbsp;operator, you create a <strong>copy of the underlying value</strong> as an<strong> encapsulated mutable value</strong>.</span></li>
<li><span style="font-family:'Segoe UI', Verdana, Arial;font-size:13px;">You can dereference a reference cell by using the&nbsp;<span class="input"><code>!</code></span>&nbsp;(bang) operator.</span>&nbsp;</li>
</ul>
<p><span style="font-family:'Segoe UI', Verdana, Arial;font-size:13px;"><span style="font-family:Helvetica, Arial, sans-serif;font-size:14px;">I bolded out the important parts. Creating new reference variable every time we increment can&#8217;t be good for your soul, but this will have to do for the time being (</span></span>Shoot me a comment if you know how to do it better.)</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d9 =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> inc a =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> c = ref a</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; incr c</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; !c</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> dec b =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> c = ref b</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; decr c</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; !c</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> (|+) a b =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> a = 0 <span style="color:blue;">then</span> b <span style="color:blue;">else</span> inc ((|+) (dec a) b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> (@+) a b =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> a = 0 <span style="color:blue;">then</span> b <span style="color:blue;">else</span> dec a @+ inc b</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> result_iter = 100000 @+ 1</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&#8220;Iterative summation result: %d&#8221;</span> result_iter</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> result_rec = 100000 |+ 1</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&#8220;Recursive summation result: %d&#8221;</span> result_iter</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;27</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;28</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore Console.ReadKey</p>
</div>
<p></p>
<p>Since I wanted to evaluate both recursive and iterative in the same module, I did name them differently, so in retrospective I could&#8217;ve used standard <code>+</code> operator in <code>inc</code> and <code>dec</code> functions. Oh well. Couple of notes here:&nbsp;</p>
<ul>
<li>100000 is a random number which seems to work for me &#8211; iteration takes not that long to finish, while recursion overflows the stack.&nbsp;</li>
<li>Note how our operators while technically commutative when executed on ideal machine are not commutative in terms of time required (both) or the result on non-ideal machine (recursive).</li>
<li>@+ as an infix operator? I &lt;3 it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp;</li>
</ul>
<p>Further reading now:&nbsp;</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Church%E2%80%93Turing_thesis">Church&ndash;Turing&nbsp;thesis</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd233186.aspx">MSDN: Reference Cells (F#)</a></li>
<li><a href="http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!170.entry?ppud=4&amp;wa=wsignin1.0&amp;sa=939783959">Catamorphisms, part one</a></li>
</ul>
<p>&nbsp;</p>
<p>Now this can be the end, if not for the <strong>Bonus Level</strong>: using <a title=".Net Reflector" href="http://www.red-gate.com/products/reflector/">.Net Reflector</a> to look inside the assembly.</p>
<p>That&#8217;s how <strong>iterative operator</strong> looks like disassembled:</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;[CompilationArgumentCounts(<span style="color:blue;">new</span> int[] { 1, 1 })]</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">public</span> <span style="color:blue;">static</span> int op_AtPlus(int a, int b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;{</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">while</span> (a != 0)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; b = inc(b);</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; a = dec(a);</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> b;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;}</p>
<p style="margin:0;">&nbsp;</p>
</div>
<p>And here&#8217;s <strong>recursive operator</strong>:</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;[CompilationArgumentCounts(<span style="color:blue;">new</span> int[] { 1, 1 })]</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">public</span> <span style="color:blue;">static</span> int op_BarPlus(int a, int b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;{</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (a != 0)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> inc(op_BarPlus(dec(a), b));</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> b;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;}</p>
</div>
<p>Now you can see the computation process transformation: iterative operator is just a <code>while</code> loop.&nbsp;</p>
<p>Btw, that&#8217;s what &#8216;<strong>reference cell</strong>&#8216; means internally:</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">public</span> <span style="color:blue;">static</span> int dec(int b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;{</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; FSharpRef&lt;int&gt; c = Operators.Ref&lt;int&gt;(b);</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; Operators.Decrement(c);</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> Operators.op_Dereference&lt;int&gt;(c);</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;}</p>
</div>
<p>(The lack of <code>[CompilationArgumentCounts]</code> is something to be left to further investigation.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/76/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=76&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/20/sicp-excercise-1-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.8</title>
		<link>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-8/</link>
		<comments>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-8/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 19:52:06 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.1]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/?p=70</guid>
		<description><![CDATA[Is a bit of the same stuff as 1.7, polishing F# typing skills. SICP Exercise 1.8 &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160;open System &#160;&#160;&#160;&#160;4&#160;module Ex1d8 = &#160;&#160;&#160;&#160;5&#160;&#160;&#160;&#160; let is_good_enough latest_guess last_guess x = &#160;&#160;&#160;&#160;6&#160;&#160;&#160;&#160; &#160;&#160;&#160; (abs (latest_guess &#8211; last_guess) &#60; (0.0001 * last_guess)) &#160;&#160;&#160;&#160;7&#160; &#160;&#160;&#160;&#160;8&#160;&#160;&#160;&#160; let improve_guess guess x = &#160;&#160;&#160;&#160;9&#160;&#160;&#160;&#160; &#160;&#160;&#160; (2.0*guess + x/(guess*guess))/3.0 &#160;&#160;&#160;10&#160; &#160;&#160;&#160;11&#160;&#160;&#160;&#160; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=70&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>Is a bit of the same stuff as 1.7, polishing F# typing skills.</div>
</p>
<div><strong>SICP Exercise 1.8</strong></div>
</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d8 = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> is_good_enough latest_guess last_guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (abs (latest_guess &#8211; last_guess) &lt; (0.0001 * last_guess))</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> improve_guess guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (2.0*guess + x/(guess*guess))/3.0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> inner_cubic_iter latest_guess last_guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> is_good_enough latest_guess last_guess x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> latest_guess</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> inner_cubic_iter (improve_guess latest_guess x) latest_guess x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> cubic_root_iter first_guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; inner_cubic_iter first_guess (improve_guess first_guess x) x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> a = 0.000000001</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[New-Calculated] Cubic root of %e is %e&quot;</span> a (cubic_root_iter 0.5 a)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> b = Double.MaxValue/2.0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[New-Calculated] Cubic root of %e is %e&quot;</span> (b) (cubic_root_iter 2.0 b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;27</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;28</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore (Console.ReadKey())</p>
</div>
</p>
<div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/70/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=70&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.7</title>
		<link>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-7/</link>
		<comments>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-7/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 19:41:13 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.1]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/?p=62</guid>
		<description><![CDATA[The distinction between mathematical function and programming procedure, in the form section 1.1.7 puts it reminds me of wave-particle duality. There was another distinction between function and procedure &#8211; in Pascal procedures are functions which do not return a result. I can&#8217;t use &#8216;?&#8217; or &#8216;-&#8217; as part of my function names (and it makes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=62&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<ul>
<li>The distinction between mathematical function and programming procedure, in the form section 1.1.7 puts it reminds me of wave-particle duality.</li>
<li>There was another distinction between function and procedure &#8211; in Pascal procedures are functions which do not return a result.</li>
<li>I can&#8217;t use &#8216;?&#8217; or &#8216;-&#8217; as part of my function names (and it makes me sad) &gt;.&lt;</li>
</ul>
</div>
<div>Anyhow.</div>
</p>
<div><strong>SICP Exercise 1.7<br />
</strong></div>
</p>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;<span style="color:blue;">module</span> Ex1d7 = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> is_good_enough_impr latest_guess last_guess = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; abs (latest_guess &#8211; last_guess) &lt; (0.0001 * latest_guess)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> sqrt_iter_inner latest_guess last_guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> is_good_enough_impr latest_guess last_guess</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> latest_guess</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> sqrt_iter_inner (Ex1d6.improve_guess latest_guess x) latest_guess x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> sqrt_iter guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> Ex1d6.is_good_enough guess x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> guess </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> sqrt_iter_inner (Ex1d6.improve_guess guess x) guess x </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> a = 0.0000000001</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[New-Calculated] Square root of %f is %f&quot;</span> a (sqrt_iter 0.5 a)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[Old-Calculated] Square root of %f is %f&quot;</span> a (Ex1d6.sqrt_iter 0.5 a)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;27</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> b = Double.MaxValue/2.0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;28</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[New-Calculated] Square root of %e is %e&quot;</span> (b) (sqrt_iter 2.0 b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;29</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printfn <span style="color:maroon;">&quot;[Old-Calculated] Square root of %e is %e&quot;</span> (b) (Ex1d6.sqrt_iter 2.0 b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;30</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;31</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore (Console.ReadKey())</p>
</div>
</p>
<div>I used Ex1d6 code here <a href="http://tvtropes.org/pmwiki/pmwiki.php/Main/BlatantLies">to promote</a> <a href="http://tvtropes.org/pmwiki/pmwiki.php/Main/TheSlacker">code reuse</a></div>
</p>
<div>So. It is fairly obvious why old version didn&#8217;t work for small numbers: if x is of the same order as our arbitrary precision s then x+-s is not a good approximation for x. In other words: absolute precision values are bad for arbitrary input in such a general function.</div>
</p>
<div>The problem with larger numbers is somewhat more tricky: it boils down to the representation of floating-point numbers in the computer memory. Floating-point number is represented as <em>+-mantissa * 2^exponent</em> and stored as <code>(sign)(mantissa)(exponent)</code>, packed in some amount of bit (i.e. float 32 &#8211; 32 bit) [It's a bit more complicated but nevermind that]. So when you are trying to calculate a square root of the large number using 1.6 version of <code>is_good_enough</code> and <code>improve_guess</code>, there will come a moment when <code>improve_guess</code> is returning the same number (&#8216;guess&#8217;) over and over again. When you square that number it&#8217;s not close enough to the target (&#8216;x&#8217;), all because you are dropping small parts of guess&#8217; mantissa on both multiplication and division. Those small parts, however, are being multiplied by 2^exponent (think ~2^153 for the &#8216;guess&#8217; for the double precision), so the difference on the <code>is_good_enough</code> check is still greater than our absolute precision value.</div>
</p>
<div>Needless to say, &#8216;stop when our guess is not changing much&#8217;, while fine for square root, is a rather bad idea in general case. But that&#8217;s a problem from a whole other (very interesting!) field &#8211; numerical methods.</div>
</p>
<div>Further reading:</div>
<div>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.double.aspx">Double Structure (MSDN)</a></li>
<li><a href="http://www.extremeoptimization.com/Resources/Articles/FPDotNetConceptsAndFormats.aspx#terms">Floating Point in .NET part 1: Concepts and Formats</a></li>
</ul>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=62&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.6</title>
		<link>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-6/</link>
		<comments>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-6/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 19:30:54 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.1]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/?p=52</guid>
		<description><![CDATA[So &#8230; unusual. Let&#8217;s take this piece of code here: &#160;&#160;&#160;&#160;1&#160;&#160;&#160;&#160; let average a b = &#160;&#160;&#160;&#160;2&#160;&#160;&#160;&#160; &#160;&#160;&#160; (a + b)/2.0 &#160;&#160;&#160;&#160;3&#160;&#160;&#160;&#160; let improve_guess guess x = &#160;&#160;&#160;&#160;4&#160;&#160;&#160;&#160; &#160;&#160;&#160; average guess x/guess The call to average in the improve_guess procedure is being parsed into (average guess x)/guess instead of (expected by me) average guess (x/guess). These are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=52&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>So &#8230; unusual.</div>
<div>Let&#8217;s take this piece of code here:</div>
<div><strong><br />
</strong></div>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> average a b =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (a + b)/2.0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> improve_guess guess x =</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; average guess x/guess</p>
</div>
<div><strong><br />
</strong></div>
<div>The call to average in the improve_guess procedure is being parsed into <code>(average guess x)/guess</code> instead of (expected by me) <code>average guess (x/guess)</code>. These are the days of a newbie.</div>
<div>Anyhow.</div>
<div><strong><br />
</strong></div>
<div><strong>SICP Exercise 1.6</strong></div>
<div><strong><br />
</strong></div>
<div>The gist of this excercise is the same as Ex 1.5: functions evaluate their arguments as they are passed. So new-if (or, as F# prefers to call it, new_if) will force evaluation for both if_clause and else_clause before making a choice. That&#8217;s why base if is a special form! (Achievement unlocked: [Aesop Learned!]). (We can probably emulate this behavior with lazy expressions).</div>
<div></div>
<div><strong><br />
</strong></div>
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color:blue;">open</span> System</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;<span style="color:blue;">module</span> Ex1d6 = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> average a b = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (a + b)/2.0</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> improve_guess guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; average guess (x/guess)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> is_good_enough (guess:double) (x:double) = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; abs (guess*guess &#8211; x) &lt; 0.0001</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> sqrt_iter guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> is_good_enough guess x </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">then</span> guess </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">else</span> sqrt_iter (improve_guess guess x) x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> new_if predicate then_clause else_clause = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">match</span> predicate <span style="color:blue;">with</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; <span style="color:blue;">true</span> <span style="color:blue;">-&gt;</span> then_clause</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; _ <span style="color:blue;">-&gt;</span> else_clause</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;27</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> new_sqrt_iter guess x = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;28</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; new_if (is_good_enough guess x) </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;29</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; guess </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;30</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (new_sqrt_iter (improve_guess guess x) x)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;31</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;32</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;33</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ignore (sqrt_iter 25.0 2.0)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;34</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ignore (new_sqrt_iter 25.0 2.0)</p>
</div>
<div><strong><br />
</strong></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=52&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.5</title>
		<link>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-5/</link>
		<comments>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-5/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 19:17:13 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.1]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/?p=42</guid>
		<description><![CDATA[SICP Exercise 1.5 There had to be some hacking with p and test function definitions to satisfy the static typing / type inference of F#. I opted for the easiest way: &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160; &#160;&#160;&#160;&#160;4&#160;module Ex1d5 = &#160;&#160;&#160;&#160;5&#160;&#160;&#160;&#160; let rec p (x:int) :int = p x &#160;&#160;&#160;&#160;6&#160; &#160;&#160;&#160;&#160;7&#160;&#160;&#160;&#160; let test x y = &#160;&#160;&#160;&#160;8&#160;&#160;&#160;&#160; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=42&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>SICP Exercise 1.5</strong></p>
<p>There had to be some hacking with p and test function definitions to satisfy the static typing / type inference of F#. I opted for the easiest way:</p>
<p />
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d5 = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> p (x:int) :int = p x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> test x y = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> x = 0 <span style="color:blue;">then</span> 0 <span style="color:blue;">else</span> y</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; test 0 (p 0)</p>
</div>
<p />
<div>
F#, GNU Scheme and DrRacket alike are evaluating second argument when passing it to the function, so they all are using applicative order of evaluation. </p>
<p>Bonus level: There is a way in C# to force lazy evaluation for the function, basically telling p that it should not evaluate unless it absolutely has to. Note how static typing makes us change function signatures here.
</p></div>
<p />
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d5BonusLevel = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> <span style="color:blue;">rec</span> p (x:int) = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">lazy</span> ( <span style="color:blue;">let</span> a = p x</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; a.Value )</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> test x (y:Lazy&lt;int&gt;) = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> x = 0 <span style="color:blue;">then</span> 0 <span style="color:blue;">else</span> y.Value</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; test 0 (p 0)</p>
</div>
<p />
<div>
This calls for further reading:</p>
<ul>
<li><a href="http://weblogs.asp.net/podwysocki/archive/2008/03/21/adventures-in-f-f-101-part-6-lazy-evaluation.aspx">F# 101 Part 6 (Lazy Evaluation)</a></li>
<li><a href="http://blogs.msdn.com/b/jomo_fisher/archive/2007/09/27/adventures-in-f-lazy-like-a-fox.aspx">Adventures in F# &#8211; Lazy Like a Fox</a></li>
</ul>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=42&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 1.4</title>
		<link>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-4/</link>
		<comments>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-4/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 18:59:53 +0000</pubDate>
		<dc:creator>ellerain</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[SICP 1]]></category>
		<category><![CDATA[SICP 1.1]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ellerain.wordpress.com/?p=26</guid>
		<description><![CDATA[SICP Exercise 1.4 Now this is an interesting beast. Instead of using a result of conditional in an expression as a operand we are trying to use it as an operator. Well, F# has prefix notation for its operators as well (sorta)! &#160;&#160;&#160;&#160;1&#160;#light &#160;&#160;&#160;&#160;2&#160;namespace SICP &#160;&#160;&#160;&#160;3&#160; &#160;&#160;&#160;&#160;4&#160;module Ex1d4 = &#160;&#160;&#160;&#160;5&#160; &#160;&#160;&#160;&#160;6&#160;&#160;&#160;&#160; let a_plus_abs_b a b [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=26&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><strong>SICP Exercise 1.4</strong></div>
<div><strong><br />
</strong></div>
<div>Now this is an interesting beast. Instead of using a result of conditional in an expression as a operand we are trying to use it as an operator. Well, F# has prefix notation for its operators as well (sorta)!</div>
<p />
<div style="font-family:Consolas;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color:blue;">#light</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color:blue;">namespace</span> SICP</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color:blue;">module</span> Ex1d4 = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> a_plus_abs_b a b = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ((<span style="color:blue;">if</span> (b &gt; 0) <span style="color:blue;">then</span> (+) <span style="color:blue;">else</span> (-)) a b)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">let</span> execute = </p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore (a_plus_abs_b 3 4)</p>
<p style="margin:0;"><span style="color:#2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ignore (a_plus_abs_b 3 -4)</p>
</div>
<p />
<div>That went well. A few notes:</div>
<div>
<ul>
<li>(+) is technically a reference to the function implementing +, the infix operator.</li>
<li>Common Lisp also forces programmer to specifically get function from operator name</li>
<li>Unlike Common Lisp though, F# does not require explicit &#8220;the result of this expression is a function, call it&#8221; funcall function.</li>
<li>F# does not allow variadic functions (functions that can have a variable number of arguments), thus (+) is strictly int-&gt;int-&gt;int (no (+) 2 3 4 for you!)</li>
</ul>
</div>
<div>Further reading:</div>
<div>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Variadic_function">Variadic function</a></li>
<li><a href="http://www.markhneedham.com/blog/2009/01/12/f-partial-function-application-with-the-function-composition-operator/">F#: Partial Function Application with the Function Composition Operator</a></li>
<li><a href="http://cs.hubfs.net/forums/thread/7120.aspx">Prefix Notation Problem</a></li>
</ul>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ellerain.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ellerain.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ellerain.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ellerain.wordpress.com&amp;blog=9791830&amp;post=26&amp;subd=ellerain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ellerain.wordpress.com/2010/08/19/sicp-excercise-1-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f79664be1b45dfd0a7a979356eb10b3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ellerain</media:title>
		</media:content>
	</item>
	</channel>
</rss>
