<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc=" http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>briancarper.net (λ) (Tag: SICP)</title><link>http://briancarper.net/tag/180/sicp</link><description>Some guy's blog about programming and Linux and cows.</description><item><title>Functional programming hurts me</title><link>http://briancarper.net/blog/functional-programming-hurts-me</link><guid>http://briancarper.net/blog/functional-programming-hurts-me</guid><pubDate>Sat, 02 Feb 2008 00:53:23 -0800</pubDate><description>&lt;p&gt;This post will probably come back to haunt me, but functional programming doesn't sit well with me.  I'm trying again to read through &lt;a href=&quot;http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html#%_toc_start&quot;&gt;SICP&lt;/a&gt;.  Last time I tried, I lost track of it and gave up after a few chapters.  This time I hope to persevere, and I'm trying to go more slowly through the examples to really understand things.&lt;/p&gt;

&lt;p&gt;I was trying to do some of the exercises in chapter 1 today, and pretty much bombing on all of them.  Most of the exercises I've seen so far in SICP sound like they came out of a math book.  &quot;&lt;em&gt;There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps...&lt;/em&gt;&quot;  Uh huh.  This is not the kind of problem I enjoy, or have even the slightest use for.  But maybe the problems are only pointless examples to illustrate a point, like &quot;mass-less friction-less pulleys&quot; in physics textbooks.  I can live with that I guess.&lt;/p&gt;

&lt;p&gt;Here is a Scheme example from SICP to compute b to the power of n:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (expt b n)
  (expt-iter b n 1))

(define (expt-iter b counter product)
  (if (= counter 0)
      product
      (expt-iter b
                (- counter 1)
                (* b product))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is presented as a wonderful and novel functional programming approach.  &lt;/p&gt;

&lt;p&gt;One of my problems is the amount of brain-work it takes to understand this.  OK, start with EXPT.  We take &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;, and then call some recursive helper function with b, n, and a state variable &lt;code&gt;product&lt;/code&gt; initialized to 1 &lt;em&gt;HEY WAIT A SECOND THERE'S A STATE VARIABLE&lt;/em&gt;?!&lt;/p&gt;

&lt;p&gt;This is one way to cheat in functional programming, I guess.  &lt;code&gt;product&lt;/code&gt; is a variable name / symbol, which describes &quot;something&quot; that takes on different values over time, each value representing the same &quot;thing&quot; in different states.  That qualifies as a state variable to me.  The only difference is that the state variable is actually N different variables (with the same name) splattered across a bunch of frames on a call stack, instead of an abstraction of a single simple bucket where I dump a value.&lt;/p&gt;

&lt;p&gt;Now in EXPT-ITER, &lt;code&gt;counter&lt;/code&gt; starts at n, and every time the function calls itself, counter decreases by 1, until you reach 0, at which point EXPT-ITER stops calling itself and returns something.  So this is a perverted, inside-out way of saying &quot;do something N times&quot;, where every iteration costs me a recursive function call.  Nice.&lt;/p&gt;

&lt;p&gt;OK, what are we doing N times?  We're multiplying &lt;code&gt;product&lt;/code&gt;, which starts at 1, by &lt;code&gt;b&lt;/code&gt;, which then becomes the new value of &lt;code&gt;product&lt;/code&gt; next time around.&lt;/p&gt;

&lt;p&gt;SO WHY NOT JUST WRITE THAT TO BEGIN WITH?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defun simple-expt (b n)
  (let ((total 1))
    (dotimes (i n)
      (setf total (* total b)))
    total))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Common Lisp this reads as &quot;&lt;em&gt;Let total start at 1.  N times, set total to the product of total and b.  Then return total.&lt;/em&gt;&quot;  This is EXACTLY what the Scheme version is doing.  Except I don't have to turn my brain inside-out to write it down this way.  The CL code reads exactly like what the program is doing.&lt;/p&gt;

&lt;p&gt;When all you have is a hammer, every problem looks like a nail. When all you have is recursion, every problem looks like it should be solved using recursion, I guess.&lt;/p&gt;

&lt;p&gt;Actually if I ignore the functional programming bits, SICP is a great book.  There are a lot of good things in there.  But (so far) Scheme isn't one of them.&lt;/p&gt;</description></item></channel></rss>

