<?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:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: SICP sections 2.1.1 &#8211; 2.1.2</title>
	<atom:link href="http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 28 Jul 2010 12:22:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Peter Michaux</title>
		<link>http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/comment-page-1/#comment-123726</link>
		<dc:creator>Peter Michaux</dc:creator>
		<pubDate>Thu, 10 Jul 2008 22:43:58 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/#comment-123726</guid>
		<description>I expressed addition of Church numerals explicitly in terms of the concept of function composition. (Note the following is Scheme code.)

(define (compose f g)
&#160;&#160;(lambda (x)
&#160;&#160;&#160;&#160;(f (g x))))

(define (add-church a b)
&#160;&#160;(lambda (f)
&#160;&#160;&#160;&#160;(compose (a f) (b f))))

In problems like this it becomes apparent that all of Common Lisp&#039;s &quot;funcall&quot; can be quite a lot of clutter.

This exercise also makes it painfully apparent that my brain needs more exercise!</description>
		<content:encoded><![CDATA[<p>I expressed addition of Church numerals explicitly in terms of the concept of function composition. (Note the following is Scheme code.)</p>
<p>(define (compose f g)<br />
&nbsp;&nbsp;(lambda (x)<br />
&nbsp;&nbsp;&nbsp;&nbsp;(f (g x))))</p>
<p>(define (add-church a b)<br />
&nbsp;&nbsp;(lambda (f)<br />
&nbsp;&nbsp;&nbsp;&nbsp;(compose (a f) (b f))))</p>
<p>In problems like this it becomes apparent that all of Common Lisp&#8217;s &#8220;funcall&#8221; can be quite a lot of clutter.</p>
<p>This exercise also makes it painfully apparent that my brain needs more exercise!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eliben</title>
		<link>http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/comment-page-1/#comment-93766</link>
		<dc:creator>eliben</dc:creator>
		<pubDate>Fri, 21 Dec 2007 16:24:22 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/#comment-93766</guid>
		<description>You are right, there is a problem with my implementation of add. Your suggestion is slightly incorrect too as it misses a funcall. 
I fixed my code to the correct solution.

Thanks for noticing!</description>
		<content:encoded><![CDATA[<p>You are right, there is a problem with my implementation of add. Your suggestion is slightly incorrect too as it misses a funcall.<br />
I fixed my code to the correct solution.</p>
<p>Thanks for noticing!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/comment-page-1/#comment-93674</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Fri, 21 Dec 2007 04:15:18 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/#comment-93674</guid>
		<description>2.6 is close, but not quite.  a is actually a lambda in a lambda, so it needs one arg, bracket, then another arg, as does b.  The way this is written, b&#039;s nested lambdas aren&#039;t getting fully &quot;resolved&quot; (not sure of terminology).

This should actually be (I hope, I don&#039;t know common lisp, only learning scheme right now):

(defun add (a b)
  (lambda (f)
    (lambda (x) (funcall (funcall a f) ((funcall b f) x) ))))

So, (funcall b f) makes sure that b is using f, and then x is passed in to the resulting function.  This will give us f(f(f(...x)))), as many times as f is present in f.  This needs to be passed to a, but a&#039;s function (its &quot;outer lambda f&quot;) has not been fixed yet, so we need to the (funcall a f) to ensure that the REAL f is also used for a.  That being done, we pass f(f(f...x))) in as its argument, thus giving f(f(f(f(f(...x)))).</description>
		<content:encoded><![CDATA[<p>2.6 is close, but not quite.  a is actually a lambda in a lambda, so it needs one arg, bracket, then another arg, as does b.  The way this is written, b&#8217;s nested lambdas aren&#8217;t getting fully &#8220;resolved&#8221; (not sure of terminology).</p>
<p>This should actually be (I hope, I don&#8217;t know common lisp, only learning scheme right now):</p>
<p>(defun add (a b)<br />
  (lambda (f)<br />
    (lambda (x) (funcall (funcall a f) ((funcall b f) x) ))))</p>
<p>So, (funcall b f) makes sure that b is using f, and then x is passed in to the resulting function.  This will give us f(f(f(&#8230;x)))), as many times as f is present in f.  This needs to be passed to a, but a&#8217;s function (its &#8220;outer lambda f&#8221;) has not been fixed yet, so we need to the (funcall a f) to ensure that the REAL f is also used for a.  That being done, we pass f(f(f&#8230;x))) in as its argument, thus giving f(f(f(f(f(&#8230;x)))).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
