<?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 section 2.2.3</title>
	<atom:link href="http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/</link>
	<description>Eli Bendersky's personal website</description>
	<pubDate>Sat, 22 Nov 2008 00:09:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: gorilych</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-133246</link>
		<dc:creator>gorilych</dc:creator>
		<pubDate>Fri, 03 Oct 2008 01:59:04 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-133246</guid>
		<description>On ex. 2.38

for any init, el1, el2, .. eln , op should have such properties that:

(el1 op (el2 op ( ..   (eln op init)))) == ((.. (init op el1) op el2) op ... ) op eln

consider the case when n = 2:

(el1 op (el2 op init)) == ((init op el1) op el2)

We need both associative and communicative properties for op to have this:

((init op el1) op el2) =&#124;if op is communicative&#124;=((el1 op init) op el2) =&#124;if op is associative&#124;= (el1 op (init el2)) =&#124;if op is communicative&#124;= (el1 op (el2 op init))</description>
		<content:encoded><![CDATA[<p>On ex. 2.38</p>
<p>for any init, el1, el2, .. eln , op should have such properties that:</p>
<p>(el1 op (el2 op ( ..   (eln op init)))) == ((.. (init op el1) op el2) op &#8230; ) op eln</p>
<p>consider the case when n = 2:</p>
<p>(el1 op (el2 op init)) == ((init op el1) op el2)</p>
<p>We need both associative and communicative properties for op to have this:</p>
<p>((init op el1) op el2) =|if op is communicative|=((el1 op init) op el2) =|if op is associative|= (el1 op (init el2)) =|if op is communicative|= (el1 op (el2 op init))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eliben</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125299</link>
		<dc:creator>eliben</dc:creator>
		<pubDate>Thu, 07 Aug 2008 16:56:20 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125299</guid>
		<description>Arturo,
Yes, you're right. Associative and not commutative. 
I've fixed the post, thanks.</description>
		<content:encoded><![CDATA[<p>Arturo,<br />
Yes, you&#8217;re right. Associative and not commutative.<br />
I&#8217;ve fixed the post, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arturo Pina</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125179</link>
		<dc:creator>Arturo Pina</dc:creator>
		<pubDate>Tue, 05 Aug 2008 13:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125179</guid>
		<description>By the way my previous comment was for exercise 2.38</description>
		<content:encoded><![CDATA[<p>By the way my previous comment was for exercise 2.38</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arturo Pina</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125110</link>
		<dc:creator>Arturo Pina</dc:creator>
		<pubDate>Mon, 04 Aug 2008 10:48:20 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-125110</guid>
		<description>Hi,

I think the requirement is that the op is associative rather than commutative. As a counterexample please consider 2/(x+y) which is commutative and produces different results depending on if you use fold-left or fold-right

What led me to think in associativity was that intuitively the functions go building the results two ops at a time but starting from the left or from the right... A formal demonstration might be produced but that's off limits for me :-)</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I think the requirement is that the op is associative rather than commutative. As a counterexample please consider 2/(x+y) which is commutative and produces different results depending on if you use fold-left or fold-right</p>
<p>What led me to think in associativity was that intuitively the functions go building the results two ops at a time but starting from the left or from the right&#8230; A formal demonstration might be produced but that&#8217;s off limits for me <img src='http://eli.thegreenplace.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wang Shengyi</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-124941</link>
		<dc:creator>Wang Shengyi</dc:creator>
		<pubDate>Fri, 01 Aug 2008 14:16:12 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-124941</guid>
		<description>Exercise 2.41
In my opinion, the requirement "ordered triple" is a mathematical terminology such as "ordered pair". It emphasizes the order of coordinate, not the order of the numbers. You can see this concept in wikipedia: http://en.wikipedia.org/wiki/Ordered_pair. This article contains generalities as "ordered n-tuples". I give my solution here (of course it's not the optimal):
(define (triple n s)
  (filter (lambda (trp)
            (and
             (eqv? s (accumulate + 0 trp))
             (not (eqv? (car trp) (cadr trp)))
             (not (eqv? (car trp) (caddr trp)))
             (not (eqv? (cadr trp) (caddr trp)))))
          (flatmap
           (lambda (i)
             (flatmap
              (lambda (j)
                (map (lambda (k) (list i j k))
                     (enumerate-interval 1 n)))
              (enumerate-interval 1 n)))
           (enumerate-interval 1 n))))</description>
		<content:encoded><![CDATA[<p>Exercise 2.41<br />
In my opinion, the requirement &#8220;ordered triple&#8221; is a mathematical terminology such as &#8220;ordered pair&#8221;. It emphasizes the order of coordinate, not the order of the numbers. You can see this concept in wikipedia: <a href="http://en.wikipedia.org/wiki/Ordered_pair" rel="nofollow">http://en.wikipedia.org/wiki/Ordered_pair</a>. This article contains generalities as &#8220;ordered n-tuples&#8221;. I give my solution here (of course it&#8217;s not the optimal):<br />
(define (triple n s)<br />
  (filter (lambda (trp)<br />
            (and<br />
             (eqv? s (accumulate + 0 trp))<br />
             (not (eqv? (car trp) (cadr trp)))<br />
             (not (eqv? (car trp) (caddr trp)))<br />
             (not (eqv? (cadr trp) (caddr trp)))))<br />
          (flatmap<br />
           (lambda (i)<br />
             (flatmap<br />
              (lambda (j)<br />
                (map (lambda (k) (list i j k))<br />
                     (enumerate-interval 1 n)))<br />
              (enumerate-interval 1 n)))<br />
           (enumerate-interval 1 n))))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eliben</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-121627</link>
		<dc:creator>eliben</dc:creator>
		<pubDate>Sun, 25 May 2008 16:24:11 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-121627</guid>
		<description>prahlad,

When you multiply a matrix by a vector (matrix at the left, which is different from vector by matrix, where the matrix is at the right), the vector must be a column vector and the multiplication is indeed the dot product of the vector and each row.</description>
		<content:encoded><![CDATA[<p>prahlad,</p>
<p>When you multiply a matrix by a vector (matrix at the left, which is different from vector by matrix, where the matrix is at the right), the vector must be a column vector and the multiplication is indeed the dot product of the vector and each row.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prahlad</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-121540</link>
		<dc:creator>prahlad</dc:creator>
		<pubDate>Sat, 24 May 2008 08:58:03 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-121540</guid>
		<description>As far as i am able to understand this procdure 
(defun matrix-*-vector (m v)
  (mapcar
    (lambda (row)
      (dot-product row v))
    m))
it gives the dot product of vector and each row of matrix. while the matrix*-vector should provide the dot product of given vector and each column of matrix.</description>
		<content:encoded><![CDATA[<p>As far as i am able to understand this procdure<br />
(defun matrix-*-vector (m v)<br />
  (mapcar<br />
    (lambda (row)<br />
      (dot-product row v))<br />
    m))<br />
it gives the dot product of vector and each row of matrix. while the matrix*-vector should provide the dot product of given vector and each column of matrix.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ElPicasso</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-95300</link>
		<dc:creator>ElPicasso</dc:creator>
		<pubDate>Sun, 30 Dec 2007 14:24:47 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-95300</guid>
		<description>thoughts about ex 2.41
unique-triples can be rewritten using unique-pairs:

unique triples is flatmap of the first element with unique-pair of (1+ n) till end

for instance:
(unique-triples 4)
; ((1 2 3) (1 2 4) (1 3 4) (2 3 4))

    first element is 1, unique-pair from 2 till 4 is: (2 3) (2 4) (3 4), so becomes (1 2 3) (1 2 4) (1 3 4)
    snd element is 2, unique-pair from 3 till 4 is: (3 4), so (2 3 4)
   
&lt;code&gt;
(define (unique-pairs-interval from till)
  (flatmap
   (lambda (i)
     (map (lambda (j) (list i j))
	  (enumerate-interval (+ 1 i) till)))
   (enumerate-interval from till)))

(define (unique-triples n)
  (flatmap
   (lambda (i)
     (map (lambda (j) (append (list i) j))
	  (unique-pairs-interval (+ 1 i) n)))
   (enumerate-interval 1 n)))

&lt;/code&gt;

even more general you can say that the unique-nth is the flatmap of the first element with the unique-(nth -1) of (+1 n) till

&lt;code&gt; 
(define (unique-nths from till nths)
  (if (= nths 0)
      (list ())
      (flatmap
       (lambda (i)
	    (map (lambda (j) (append (list i) j))
	          (unique-nths (+ 1 i) till (- nths 1))))
       (enumerate-interval from till))))

(define (unique-pair n)
  (unique-nths 1 n 2))

(define (unique-triples n)
    (unique-nths 1 n 3))
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>thoughts about ex 2.41<br />
unique-triples can be rewritten using unique-pairs:</p>
<p>unique triples is flatmap of the first element with unique-pair of (1+ n) till end</p>
<p>for instance:<br />
(unique-triples 4)<br />
; ((1 2 3) (1 2 4) (1 3 4) (2 3 4))</p>
<p>    first element is 1, unique-pair from 2 till 4 is: (2 3) (2 4) (3 4), so becomes (1 2 3) (1 2 4) (1 3 4)<br />
    snd element is 2, unique-pair from 3 till 4 is: (3 4), so (2 3 4)</p>
<p><code><br />
(define (unique-pairs-interval from till)<br />
  (flatmap<br />
   (lambda (i)<br />
     (map (lambda (j) (list i j))<br />
	  (enumerate-interval (+ 1 i) till)))<br />
   (enumerate-interval from till)))</p>
<p>(define (unique-triples n)<br />
  (flatmap<br />
   (lambda (i)<br />
     (map (lambda (j) (append (list i) j))<br />
	  (unique-pairs-interval (+ 1 i) n)))<br />
   (enumerate-interval 1 n)))</p>
<p></code></p>
<p>even more general you can say that the unique-nth is the flatmap of the first element with the unique-(nth -1) of (+1 n) till</p>
<p><code><br />
(define (unique-nths from till nths)<br />
  (if (= nths 0)<br />
      (list ())<br />
      (flatmap<br />
       (lambda (i)<br />
	    (map (lambda (j) (append (list i) j))<br />
	          (unique-nths (+ 1 i) till (- nths 1))))<br />
       (enumerate-interval from till))))</p>
<p>(define (unique-pair n)<br />
  (unique-nths 1 n 2))</p>
<p>(define (unique-triples n)<br />
    (unique-nths 1 n 3))<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rinat</title>
		<link>http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-77553</link>
		<dc:creator>rinat</dc:creator>
		<pubDate>Sat, 13 Oct 2007 07:55:18 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/2007/08/22/sicp-sections-223/#comment-77553</guid>
		<description>ex 2.35
&lt;code&gt;
(define (count-leaves-acc t)
  (accumulate + 0 (map (lambda (x) (+ 0 1)) 
                       (enumerate-tree t))))
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>ex 2.35<br />
<code><br />
(define (count-leaves-acc t)<br />
  (accumulate + 0 (map (lambda (x) (+ 0 1))<br />
                       (enumerate-tree t))))<br />
</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
