<?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"
	>
<channel>
	<title>Comments on: SICP sections 2.1.1 - 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>
	<pubDate>Thu, 04 Dec 2008 01:18:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Peter Michaux</title>
		<link>http://eli.thegreenplace.net/2007/07/25/sicp-sections-211-212/#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's "funcall" 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-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-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's nested lambdas aren't getting fully "resolved" (not sure of terminology).

This should actually be (I hope, I don'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's function (its "outer lambda f") 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>
