<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Eric Tobia's Blog - SICP</title>
    <link>http://www.erictobia.com/</link>
    <description>People, places, and things (via HTTP)</description>
    <language>en-us</language>
    <copyright>Eric Tobia</copyright>
    <lastBuildDate>Mon, 03 Dec 2007 00:01:16 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>blog@erictobia.com</managingEditor>
    <webMaster>blog@erictobia.com</webMaster>
    <item>
      <trackback:ping>http://www.erictobia.com/Trackback.aspx?guid=60c00cc8-b378-4b9b-a75a-ffb785cf39c9</trackback:ping>
      <pingback:server>http://www.erictobia.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.erictobia.com/PermaLink,guid,60c00cc8-b378-4b9b-a75a-ffb785cf39c9.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.erictobia.com/CommentView,guid,60c00cc8-b378-4b9b-a75a-ffb785cf39c9.aspx</wfw:comment>
      <wfw:commentRss>http://www.erictobia.com/SyndicationService.asmx/GetEntryCommentsRss?guid=60c00cc8-b378-4b9b-a75a-ffb785cf39c9</wfw:commentRss>
      <title>SICP in C# 3.0 – Higher-Order Procedures</title>
      <guid isPermaLink="false">http://www.erictobia.com/PermaLink,guid,60c00cc8-b378-4b9b-a75a-ffb785cf39c9.aspx</guid>
      <link>http://www.erictobia.com/2007/12/03/SICPInC30HigherOrderProcedures.aspx</link>
      <pubDate>Mon, 03 Dec 2007 00:01:16 GMT</pubDate>
      <description>&lt;img src="http://www.erictobia.com/content/binary/lambda1.gif" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
One of my favorite sections in &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book.html"&gt;SICP&lt;/a&gt; so
far has been section &lt;a href="ttp://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3"&gt;1.3&lt;span style=""&gt; &lt;/span&gt;Formulating
Abstractions with Higher-Order Procedures&lt;/a&gt;.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Higher-order_function"&gt;Higher-Order
Procedures&lt;/a&gt; are procedures that deal with other procedures.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;They
can take a procedure as an argument and/or return a procedure as a result. 
&lt;p&gt;
Though it is somewhat academic, I really like the example that &lt;a href="http://en.wikipedia.org/wiki/Hal_Abelson"&gt;Abelson&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Gerald_Jay_Sussman"&gt;Sussman&lt;/a&gt; use
in section 1.3.1 to explain the “procedures as arguments” scenario.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;To
illustrate this concept they implement summation &lt;img src="http://www.erictobia.com/content/binary/sigma.gif" border="0"&gt; using
Scheme.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;They implement a higher-order procedure called
“sum” that takes two procedures and a range of numbers as arguments.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;One
procedure is used for computing the value that is added to the sum and the other is
used to determine the next value to compute.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;The Scheme
code can be seen &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.1"&gt;here&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
&lt;o:p&gt;&lt;/o:p&gt;
It’s not as useful as &lt;a href="http://en.wikipedia.org/wiki/Map_%28higher-order_function%29"&gt;map&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29"&gt;reduce&lt;/a&gt;,
but those are not discussed until Chapter 2. 
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;o:p&gt;&lt;/o:p&gt;
As some of you may know C# 3.0 adds a whole slew of functional/dynamic features.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;Whether
this is an effort to keep up with the &lt;a href="http://www.ruby-lang.org/en/"&gt;cool&lt;/a&gt; &lt;a href="http://www.python.org/"&gt;kids&lt;/a&gt; or
merely a necessity for making &lt;a href="http://en.wikipedia.org/wiki/LINQ"&gt;LINQ&lt;/a&gt; work
is not important to me.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;What I’m curious to know is if
the features make it easier to implement the concepts in SICP using C# 3.0.
&lt;/p&gt;
&lt;p&gt;
&lt;b style=""&gt;&lt;u&gt;Implementing “Sum” in C# 3.0&lt;/u&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
Following along with the example from SICP - let’s say that you have defined the following
methods.
&lt;/p&gt;
&lt;span style=""&gt;&lt;/span&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;img src="http://www.erictobia.com/content/binary/sumcode1.gif" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
I decided to break with the book here and use iteration instead of recursion to make
the code a little more digestible. 
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Common to each method is the idea of iterating over a range of numbers and adding
the result of each computation to the total.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;If we create
a higher-order procedure called “Sum” to abstract out this behavior we can then redefine
each method.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;img src="http://www.erictobia.com/content/binary/sumcode2.gif" border="0"&gt;&lt;/pre&gt;
The “=&amp;gt;” and “Func” constructs come from the new &lt;i style=""&gt;Lambda expressions&lt;/i&gt; feature
in C#.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;Lambda expressions are a great new aspect of the
language and they make implementing higher-order procedures a lot easier.&amp;nbsp; In
this case the "term" and "next" arguments can be passed as Lambda expressions.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
I had to cheat and do some casting &lt;a href="http://en.wikipedia.org/wiki/Shenanigans"&gt;shenanigans&lt;/a&gt; because
the division operation in "PiSum()" brings floating point numbers into the mix, but
I feel like this code does capture the essence of the original Scheme implementation.&lt;span style=""&gt;&amp;nbsp;
Implementing "Sum" as a &lt;a href="http://msdn2.microsoft.com/en-us/library/twcad0zb%28VS.80%29.aspx"&gt;generic
method&lt;/a&gt; might be a way to make the code more elegant.&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;span style=""&gt;&lt;/span&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
If subverting the type system does not appeal to you, the following JavaScript implementation
might be of interest.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.erictobia.com/content/binary/sumcode3.gif" border="0"&gt;
&lt;br&gt;
&lt;/p&gt;
JavaScript is more similar to Scheme (dynamic typing/first class functions) so it
makes sense that the translation is smoother.&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
It turns out that there are other people interested in implementing SICP in different
languages.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;&lt;a href="http://www.codepoetics.com/wiki/index.php?title=Topics:SICP_in_other_languages"&gt;Chris
Rathman&lt;/a&gt; has translated parts of SICP into 19 other languages.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;One
disturbing thing to note about Chris’s C# translation is that it doesn’t go beyond
chapter 1.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;Does that mean that it is not possible to implement
the code from the rest of the book in C#?&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-family: Wingdings;"&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.erictobia.com/aggbug.ashx?id=60c00cc8-b378-4b9b-a75a-ffb785cf39c9" /&gt;</description>
      <comments>http://www.erictobia.com/CommentView,guid,60c00cc8-b378-4b9b-a75a-ffb785cf39c9.aspx</comments>
      <category>C# 3.0</category>
      <category>Programming</category>
      <category>SICP</category>
    </item>
  </channel>
</rss>