<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Weiqun Zhang&#039;s Blog</title>
	<atom:link href="http://wqzhang.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://wqzhang.wordpress.com</link>
	<description>Programming and random stuff</description>
	<lastBuildDate>Wed, 11 Jan 2012 00:57:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='wqzhang.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/95fe254425f48864dba90547da28fe80?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Weiqun Zhang&#039;s Blog</title>
		<link>http://wqzhang.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://wqzhang.wordpress.com/osd.xml" title="Weiqun Zhang&#039;s Blog" />
	<atom:link rel='hub' href='http://wqzhang.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SICP Exercise 4.49</title>
		<link>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-49/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-49/#comments</comments>
		<pubDate>Sat, 01 May 2010 03:10:36 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1493</guid>
		<description><![CDATA[SICP Exercise 4.49 (define (parse-word word-list) (list (car word-list) (an-element-of (cdr word-list)))) ; the results the student studies the student studies for the student the student studies for the student for the student the student studies for the student for the student for the student ......<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1493&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.49">SICP Exercise 4.49</a></p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">parse-word</span> word-list)
  (list (car word-list) (an-element-of (cdr word-list))))

<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">the results
</span>the student studies
the student studies for the student
the student studies for the student for the student
the student studies for the student for the student for the student
......
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1493/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1493&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-49/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.48</title>
		<link>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-48/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-48/#comments</comments>
		<pubDate>Sat, 01 May 2010 03:06:02 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1490</guid>
		<description><![CDATA[SICP Exercise 4.48 (define adjectives '(adjective nice tall diligent small white)) (define (parse-simple-noun-phrase) (define (parse-adjective-noun) (amb (parse-word nouns) (list 'adjective-noun (parse-word adjectives) (parse-adjective-noun)))) (list 'simple-noun-phrase (parse-word articles) (parse-adjective-noun))) ;;; Amb-Eval input: (parse '(the tall professor lectures)) ;;; Starting a new problem ;;; Amb-Eval value: (sentence (simple-noun-phrase (article the) (adjective-noun (adjective tall) (noun professor))) (verb lectures)) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1490&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.48">SICP Exercise 4.48</a></p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> <span class="scheme-function-name">adjectives</span> '(adjective nice tall diligent small white))

(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">parse-simple-noun-phrase</span>)
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">parse-adjective-noun</span>)
    (amb (parse-word nouns)
         (list 'adjective-noun
               (parse-word adjectives)
               (parse-adjective-noun))))
  (list 'simple-noun-phrase
        (parse-word articles)
        (parse-adjective-noun)))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval input:
</span>(parse '(the tall professor lectures))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Starting a new problem
</span><span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval value:
</span>(sentence
 (simple-noun-phrase
  (article the)
  (adjective-noun (adjective tall) (noun professor)))
 (verb lectures))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval input:
</span>try-again

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">There are no more values of
</span>(parse (quote (the tall professor lectures)))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval input:
</span>(parse '(the nice tall professor lectures))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Starting a new problem
</span><span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval value:
</span>(sentence
 (simple-noun-phrase
  (article the)
  (adjective-noun (adjective nice)
                  (adjective-noun (adjective tall)
                                  (noun professor))))
 (verb lectures))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval input:
</span>try-again

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">There are no more values of
</span>(parse (quote (the nice tall professor lectures)))

<span class="scheme-comment-delimiter">;;; </span><span class="scheme-comment">Amb-Eval input:
</span>(parse '(the nice tall professor lectures
             to the diligent student in the small class
             with the white cat))
<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">It works.  Output omitted
</span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1490/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1490&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/30/sicp-exercise-4-48/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.47</title>
		<link>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-47/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-47/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 02:13:10 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1484</guid>
		<description><![CDATA[SICP Exercise 4.47 It does not work correctly even for the most simple sentences such as &#8220;the professor lectures&#8221;. For the above sentence, the parser can produce the correct result when it runs for the first time. But if one runs try-again, it will be stuck in an infinite loop because the evaluation of (parse-prepositional-phrase) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1484&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.47">SICP Exercise 4.47</a></p>
<p>It does not work correctly even for the most simple sentences such as &#8220;the professor lectures&#8221;.  For the above sentence, the parser can produce the correct result when it runs for the first time.  But if one runs <code>try-again</code>, it will be stuck in an infinite loop because the evaluation of <code>(parse-prepositional-phrase)</code> always fails.  The failure will cause the evaluation of <code>(list 'verb-phrase (parse-verb-phrase) (parse-prepositional-phrase))</code>, which in turn will cause the the evaluation of <code>(parse-prepositional-phrase)</code> again.  Moreover, if the sentence contains a misspelled word, the parser will be stuck too.</p>
<p>Changing the order of expressions in the amb would make things even worse.  It would fail for the above simple sentence even in the first round.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1484/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1484&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-47/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.46</title>
		<link>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-46/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-46/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 02:04:09 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1481</guid>
		<description><![CDATA[SICP Exercise 4.46 The procedure (parse-sentence) contains (list 'sentence (parse-noun-phrase) (parse-verb-phrase)). The evaluator will evaluate (parse-verb-phrase) first, if it evaluates the arguments of list from right to left. This will fail because a sentence starts with a noun phrase, not a verb phrase.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1481&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.46">SICP Exercise 4.46</a></p>
<p>The procedure <code>(parse-sentence)</code> contains <code>(list 'sentence (parse-noun-phrase) (parse-verb-phrase))</code>.  The evaluator will evaluate <code>(parse-verb-phrase)</code> first,  if it evaluates the arguments of <code>list</code> from right to left.   This will fail because a sentence starts with a noun phrase, not a verb phrase.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1481/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1481&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-46/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.45</title>
		<link>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-45/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-45/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 04:43:36 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1471</guid>
		<description><![CDATA[SICP Exercise 4.45 The cat has three possibilities: with the professor, the student, or the class. Either the professor or the student is in the class. So there are 6 possibilities. However, if the cat is with the student, the word &#8220;class&#8221; must belong to the student because it is between the words &#8220;student&#8221; and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1471&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.45">SICP Exercise 4.45</a></p>
<p>The cat has three possibilities: with the professor, the student, or the class.  Either the professor or the student is in the class.  So there are 6 possibilities.  However, if the cat is with the student, the word &#8220;class&#8221; must belong to the student because it is between the words &#8220;student&#8221; and &#8220;cat&#8221;.  Thus there are 5 ways to parse the sentence.</p>
<ol>
<li>
    With the cat, the professor lectures in the class.  This means</p>
<ul>
<li>The cat is with the professor.</li>
<li>The professor is in the class.</li>
</ul>
</li>
<li>
    The professor lectures in the class that has the cat.  This means</p>
<ul>
<li>The cat is with the class.</li>
<li>The professor is in the class.</li>
</ul>
</li>
<li>
    With the cat, the professor lectures to student who is in the class.  This means</p>
<ul>
<li>The cat is with the professor.</li>
<li>The student is in the class.</li>
</ul>
</li>
<li>
    The professor lectures to the student in the class who has the cat.  This means</p>
<ul>
<li>The cat is with the student.</li>
<li>The student is in the class.</li>
</ul>
</li>
<li>
    The professor lectures to student who is in the class that has the cat.  This means</p>
<ul>
<li>The cat is with the class.</li>
<li>The student is in the class.</li>
</ul>
</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1471&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/29/sicp-exercise-4-45/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.44</title>
		<link>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-44/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-44/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 01:57:44 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1467</guid>
		<description><![CDATA[SICP Exercise 4.44 (define (safe? positions) (define (two-queens-safe? q1 q2) (let ((row1 (car q1)) (col1 (cadr q1)) (row2 (car q2)) (col2 (cadr q2))) (and (not (= row1 row2)) (not (= (- col2 col1) (abs (- row2 row1))))))) (let ((new-queen (list (last positions) (length positions)))) (define (check col positions) (cond ((null? (cdr positions)) true) ((two-queens-safe? (list [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1467&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.44">SICP Exercise 4.44</a></p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">safe?</span> positions)
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">two-queens-safe?</span> q1 q2)
    (<span class="scheme-keyword">let</span> ((row1 (car q1))
          (col1 (cadr q1))
          (row2 (car q2))
          (col2 (cadr q2)))
      (<span class="scheme-keyword">and</span> (not (= row1 row2))
           (not (= (- col2 col1)
                   (abs (- row2 row1)))))))
  (<span class="scheme-keyword">let</span> ((new-queen (list (last positions) (length positions))))
    (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">check</span> col positions)
      (<span class="scheme-keyword">cond</span> ((null? (cdr positions)) true)
            ((two-queens-safe? (list (car positions) col)
                               new-queen)
             (check (+ col 1) (cdr positions)))
            (<span class="scheme-keyword">else</span> false)))
    (check 1 positions)))

(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">eight-queens</span>)
  (<span class="scheme-keyword">let</span> ((q1 (amb 1 2 3 4 5 6 7 8)))
    (<span class="scheme-keyword">let</span> ((q2 (amb 1 2 3 4 5 6 7 8)))
      (require (safe? (list q1 q2)))
      (<span class="scheme-keyword">let</span> ((q3 (amb 1 2 3 4 5 6 7 8)))
        (require (safe? (list q1 q2 q3)))
        (<span class="scheme-keyword">let</span> ((q4 (amb 1 2 3 4 5 6 7 8)))
          (require (safe? (list q1 q2 q3 q4)))
          (<span class="scheme-keyword">let</span> ((q5 (amb 1 2 3 4 5 6 7 8)))
            (require (safe? (list q1 q2 q3 q4 q5)))
            (<span class="scheme-keyword">let</span> ((q6 (amb 1 2 3 4 5 6 7 8)))
              (require (safe? (list q1 q2 q3 q4 q5 q6)))
              (<span class="scheme-keyword">let</span> ((q7 (amb 1 2 3 4 5 6 7 8)))
                (require
                 (safe? (list q1 q2 q3 q4 q5 q6 q7)))
                (<span class="scheme-keyword">let</span> ((q8 (amb 1 2 3 4 5 6 7 8)))
                  (<span class="scheme-keyword">let</span> ((queens (list q1 q2 q3 q4 q5 q6 q7 q8)))
                    (require (safe? queens))
                    queens))))))))))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1467&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-44/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.43</title>
		<link>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-43/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-43/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 01:41:44 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1464</guid>
		<description><![CDATA[SICP Exercise 4.43 (define (yacht-puzzle) ; father = (last-name daughter yacht) (define (last-name f) (car f)) (define (daughter f) (cadr f)) (define (yacht f) (caddr f)) (define (different? f) (not (eq? (daughter f) (yacht f)))) (define (father-of d fathers) (if (eq? d (daughter (car fathers))) (car fathers) (father-of d (cdr fathers)))) (let ((Downing (list 'Downing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1464&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.43">SICP Exercise 4.43</a></p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">yacht-puzzle</span>)
<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">father = (last-name daughter yacht)
</span>  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">last-name</span> f) (car f))
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">daughter</span> f) (cadr f))
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">yacht</span> f) (caddr f))
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">different?</span> f) (not (eq? (daughter f) (yacht f))))
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">father-of</span> d fathers)
    (<span class="scheme-keyword">if</span> (eq? d (daughter (car fathers)))
        (car fathers)
        (father-of d (cdr fathers))))
  (<span class="scheme-keyword">let</span> ((Downing (list 'Downing
                       (amb 'Gabrielle 'Lorna 'Mary 'Rosalind)
                       'Melissa))
        (Hall (list 'Hall
                    (amb 'Gabrielle 'Lorna 'Mary 'Melissa)
                    'Rosalind))
        (Hood (list 'Hood 'Melissa 'Gabrielle))
        (Moore (list 'Moore 'Mary 'Lorna))
        (Parker (list 'Parker
                      (amb 'Gabrielle 'Lorna 'Mary 'Melissa
                           'Rosalind)
                      (amb 'Gabrielle 'Lorna 'Mary 'Melissa
                           'Rosalind))))
    (require (different? Parker))
    (<span class="scheme-keyword">let</span> ((fathers (list Downing Hall Hood Moore Parker)))
      (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">map</span> proc a)
        (<span class="scheme-keyword">if</span> (null? a)
            '()
            (cons (proc (car a)) (<span class="scheme-keyword">map</span> proc (cdr a)))))
      (<span class="scheme-keyword">let</span> ((daughters (<span class="scheme-keyword">map</span> daughter fathers)))
        (require (distinct? daughters))
        (<span class="scheme-keyword">let</span> ((yachts (<span class="scheme-keyword">map</span> yacht fathers)))
          (require (distinct? yachts))
          (require (eq? (daughter Parker)
                        (yacht (father-of 'Gabrielle fathers))))
          fathers)))))
</pre>
<p>Lorna is a daughter of Downing.</p>
<pre class="wqz-code">
<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">father  daught    yacht
</span>((downing lorna     melissa)
 (hall    gabrielle rosalind)
 (hood    melissa   gabrielle)
 (moore   mary      lorna)
 (parker  rosalind  mary))
</pre>
<p>The following code does not assume Mary&#8217;s last name is Moore. </p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">yacht-puzzle-2</span>)
  ......
        (Moore (list 'Moore
                     (amb 'Gabrielle 'Mary 'Melissa 'Rosalind)
                     'Lorna))
  ......
)
</pre>
<p>There are two solutions. The first is the same as before. The second is,</p>
<pre class="wqz-code">
<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">father  daught   yacht
</span>((downing rosalind  melissa)
 (hall    mary      rosalind)
 (hood    melissa   gabrielle)
 (moore   gabrielle lorna)
 (parker  lorna     mary)))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1464/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1464/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1464&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/28/sicp-exercise-4-43/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.42</title>
		<link>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-42/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-42/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 01:49:10 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1458</guid>
		<description><![CDATA[SICP Exercise 4.42 (define (liars) (define (xor a b) (or (and a (not b)) (and (not a) b))) (let ((betty (amb 1 2 3 4 5)) (ethel (amb 1 2 3 4 5)) (joan (amb 1 2 3 4 5)) (kitty (amb 1 2 3 4 5)) (mary (amb 1 2 3 4 5))) (require [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1458&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.42">SICP Exercise 4.42</a></p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">liars</span>)
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">xor</span> a b)
    (<span class="scheme-keyword">or</span> (<span class="scheme-keyword">and</span> a (not b)) (<span class="scheme-keyword">and</span> (not a) b)))
  (<span class="scheme-keyword">let</span> ((betty (amb 1 2 3 4 5))
        (ethel (amb 1 2 3 4 5))
        (joan (amb 1 2 3 4 5))
        (kitty (amb 1 2 3 4 5))
        (mary (amb 1 2 3 4 5)))
    (require
     (distinct? (list betty ethel joan kitty mary)))
    (require (xor (= kitty 2) (= betty 3)))
    (require (xor (= ethel 1) (= joan 2)))
    (require (xor (= joan 3) (= ethel 5)))
    (require (xor (= kitty 2) (= mary 4)))
    (require (xor (= mary 4) (= betty 1)))
    (list (list 'betty betty)
          (list 'ethel ethel)
          (list 'joan joan)
          (list 'kitty kitty)
          (list 'mary mary))))

<span class="scheme-comment-delimiter">; </span><span class="scheme-comment">the result
</span>((betty 3) (ethel 5) (joan 2) (kitty 1) (mary 4))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1458/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1458&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-42/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.41</title>
		<link>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-41/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-41/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 01:46:01 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1455</guid>
		<description><![CDATA[SICP Exercise 4.41 The next-permutation procedure below is coded according to a Wikipedia article on permutation. (define (next-permutation a-list) (let ((n (length a-list))) (define (index-1) (define (find-index-1 k) (cond ((&#60; k 0) '()) ; no such index exists ((&#60; (list-ref a-list k) (list-ref a-list (+ k 1))) k) (else (find-index-1 (- k 1))))) (find-index-1 (- [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1455&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.41">SICP Exercise 4.41</a></p>
<p>The <code>next-permutation</code> procedure below is coded according to a Wikipedia article on <a href="http://en.wikipedia.org/wiki/Permutation#Systematic_generation_of_all_permutations">permutation</a>.</p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">next-permutation</span> a-list)
  (<span class="scheme-keyword">let</span> ((n (length a-list)))
    (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">index-1</span>)
      (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">find-index-1</span> k)
        (<span class="scheme-keyword">cond</span> ((&lt; k 0)
               '()) <span class="scheme-comment-delimiter">; </span><span class="scheme-comment">no such index exists
</span>              ((&lt; (list-ref a-list k) (list-ref a-list (+ k 1)))
               k)
              (<span class="scheme-keyword">else</span>
               (find-index-1 (- k 1)))))
      (find-index-1 (- n 2)))
    (<span class="scheme-keyword">let</span> ((j (index-1)))
      (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">index-2</span>)
        (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">find-index-2</span> k)
          (<span class="scheme-keyword">if</span> (&lt; (list-ref a-list j) (list-ref a-list k))
              k
              (find-index-2 (- k 1))))
        (find-index-2 (- n 1)))
      (<span class="scheme-keyword">if</span> (null? j)
          '() <span class="scheme-comment-delimiter">; </span><span class="scheme-comment">the last permutation
</span>          (<span class="scheme-keyword">let</span> ((l (index-2)))
            (<span class="scheme-keyword">let</span> ((new-list (list-swap a-list j l)))
              (append (list-head new-list (+ j 1))
                      (reverse (list-tail new-list (+ j 1))))))))))

(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">list-swap</span> a i j)
  (<span class="scheme-keyword">if</span> (= i j)
      a
      (<span class="scheme-keyword">let</span> ((ii (min i j))
            (jj (max i j)))
        (<span class="scheme-keyword">let</span> ((ai (list-ref a ii))
              (aj (list-ref a jj)))
          (append (sublist a 0 ii)
                  (list aj)
                  (sublist a (+ ii 1) jj)
                  (list ai)
                  (list-tail a (+ jj 1)))))))

(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">multiple-dwelling</span>)
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">try</span> plan)
    (<span class="scheme-keyword">if</span> (pair? plan) <span class="scheme-comment-delimiter">; </span><span class="scheme-comment">valid?
</span>        (<span class="scheme-keyword">begin</span>
          (<span class="scheme-keyword">if</span> (good? plan)
              (<span class="scheme-keyword">begin</span>
                (display (<span class="scheme-keyword">map</span> (<span class="scheme-keyword">lambda</span> (name floor)
                                (list name floor))
                              '(baker cooper fletcher miller smith)
                              plan))
                (newline)))
          (try (next-permutation plan)))))
  (<span class="scheme-keyword">define</span> (<span class="scheme-function-name">good?</span> plan)
    (<span class="scheme-keyword">let</span> ((baker (first plan))
          (cooper (second plan))
          (fletcher (third plan))
          (miller (fourth plan))
          (smith (fifth plan)))
      (<span class="scheme-keyword">and</span> (&gt; miller cooper)
           (not (= (abs (- fletcher cooper)) 1))
           (not (= (abs (- smith fletcher)) 1))
           (not (= baker 5))
           (not (= cooper 1))
           (not (= fletcher 1))
           (not (= fletcher 5)))))
  (try '(1 2 3 4 5)))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1455&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/27/sicp-exercise-4-41/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
		<item>
		<title>SICP Exercise 4.40</title>
		<link>http://wqzhang.wordpress.com/2010/04/23/sicp-exercise-4-40/</link>
		<comments>http://wqzhang.wordpress.com/2010/04/23/sicp-exercise-4-40/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 01:40:34 +0000</pubDate>
		<dc:creator>Weiqun</dc:creator>
				<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://wqzhang.wordpress.com/?p=1451</guid>
		<description><![CDATA[SICP Exercise 4.40 The following code is about 20 times faster than the original code. (define (multiple-dwelling) (let ((cooper (amb 2 3 4 5)) (miller (amb 1 2 3 4 5))) (require (&#62; miller cooper)) (let ((fletcher (amb 2 3 4))) (require (not (= (abs (- fletcher cooper)) 1))) (let ((smith (amb 1 2 3 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1451&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_thm_4.40">SICP Exercise 4.40</a></p>
<p>The following code is about 20 times faster than the original code.</p>
<pre class="wqz-code">
(<span class="scheme-keyword">define</span> (<span class="scheme-function-name">multiple-dwelling</span>)
  (<span class="scheme-keyword">let</span> ((cooper (amb 2 3 4 5))
        (miller (amb 1 2 3 4 5)))
    (require (&gt; miller cooper))
    (<span class="scheme-keyword">let</span> ((fletcher (amb 2 3 4)))
      (require (not (= (abs (- fletcher cooper)) 1)))
      (<span class="scheme-keyword">let</span> ((smith (amb 1 2 3 4 5)))
        (require (not (= (abs (- smith fletcher)) 1)))
        (<span class="scheme-keyword">let</span> ((baker (amb 1 2 3 4)))
          (require
           (distinct? (list baker cooper fletcher miller smith)))
          (list (list 'baker baker)
                (list 'cooper cooper)
                (list 'fletcher fletcher)
                (list 'miller miller)
                (list 'smith smith)))))))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/wqzhang.wordpress.com/1451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/wqzhang.wordpress.com/1451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/wqzhang.wordpress.com/1451/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wqzhang.wordpress.com&amp;blog=995424&amp;post=1451&amp;subd=wqzhang&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://wqzhang.wordpress.com/2010/04/23/sicp-exercise-4-40/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/52919cfb59dbe5965a0ec5b49dca9ed7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Weiqun</media:title>
		</media:content>
	</item>
	</channel>
</rss>
