<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss version="2.0"><channel><title>Azeem's Blog</title><link>http://azeemblog.appspot.com/blog</link><description>The Weblog of Azeem Arshad</description><lastBuildDate>Thu, 23 Jul 2009 01:55:49 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/azeemblog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="azeemblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>N Queens Puzzle</title><link>http://azeemblog.appspot.com/blog/entry/n-queens-puzzle</link><description>&lt;p&gt;I've been reading this great book, &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book.html"&gt;Structure and Interpretation 
of Computer Programs&lt;/a&gt; for the last few days. The examples are full of really cool math, algorithms, techniques and stuff. Here is an exercise that i managed to solve in scheme, The &lt;a href="http://en.wikipedia.org/wiki/N-queens"&gt;Eight queens puzzle&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The eight queens puzzle is the problem of putting eight chess queens on an 8×8 chessboard such that none of them is able to capture any other using the standard chess queen's moves.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here is the source.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (range a b)
  (if (= a b)
      null
      (cons a (range (+ a 1) b))))

(define (accumulate op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence)
          (accumulate op initial (cdr sequence)))))

(define (flatmap proc seq)
  (accumulate append null (map proc seq)))

(define (enumerate sequence)
  (define (iter counter sequence)
    (if (null? sequence)
        null
        (append (list (cons counter (car sequence))) (iter (+ counter 1) (cdr sequence)))))
  (iter 0 sequence))

(define (rook-attack-safe? possibilities)
  (accumulate (lambda (a b) (and a b))
              true
              (map (lambda (possibility)
                     (not (= possibility (last possibilities))))
                   (cdr (reverse possibilities)))))

(define (bishop-attack-safe? possibilities)
  (accumulate (lambda (a b) (and a b))
              true
              (map (lambda (pair)
                     (not (= (abs (- (last possibilities) (cdr pair)))
                             (abs (- (- (length possibilities) 1) (car pair))))))
                   (enumerate (reverse (cdr (reverse possibilities)))))))

(define (safe? possibilities)
  (and (rook-attack-safe? possibilities)
       (bishop-attack-safe? possibilities)))

(define (n-queens-puzzle k)
  (define (solve n k)
    (if (= n 0)
        '(())
        (filter safe?
               (flatmap (lambda (last)
                          (map (lambda (item)
                                 (append item (list last)))
                               (solve (- n 1) k)))
                        (range 0 k)))))
  (solve k k))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Please excuse the crappy code. Im a total noob schemer, infact this is probably my first real scheme program. The solution is based on the recursive method described in the book. It works like this: only one queen can be placed in any one column, the problem of placing queens in all columns without any check can be reduced, using recursion, to that of placing a queen in the last column of a board in which all the other columns already have queens in positions that do not check each other.&lt;/p&gt;

&lt;p&gt;It took quite some time to solve an 8x8 board. Im not even sure if the results are correct. But a few random checks on paper turned out correct and i got 92 results, which according to wikipedia is the total number of solutions. However, there are only 12 unique solutions, many of the 92 solutions are repetitions since one solution can be horizontally or vertically flipped to create different solutions. Each solution here is a list whose indices correspond to the board column number and the values represent the row in which the queen is placed in the corresponding column.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((3 1 6 2 5 7 4 0)
 (4 1 3 6 2 7 5 0)
 (2 4 1 7 5 3 6 0)
 (2 5 3 1 7 4 6 0)
 (4 6 0 2 7 5 3 1)
 (3 5 7 2 0 6 4 1)
 (2 5 7 0 3 6 4 1)
 (4 2 7 3 6 0 5 1)
 (4 6 3 0 2 7 5 1)
 (3 0 4 7 5 2 6 1)
 (2 5 3 0 7 4 6 1)
 (3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(3 7 4 2 0 6 1 5)
 (2 4 7 3 0 6 1 5)
 (3 1 7 4 6 0 2 5)
 (4 6 1 3 7 0 2 5)
 (6 3 1 4 7 0 2 5)
 (7 1 3 0 6 4 2 5)
 (6 1 3 0 7 4 2 5)
 (4 0 7 3 1 6 2 5)
 (3 0 4 7 1 6 2 5)
 (4 1 7 0 3 6 2 5)
 (2 6 1 7 4 0 3 5)
 (2 0 6 4 7 1 3 5)
 (7 1 4 2 0 6 3 5)
 (2 4 1 7 0 6 3 5)
 (2 4 6 0 3 1 7 5)
 (4 1 3 5 7 2 0 6)
 (5 2 4 7 0 3 1 6)
 (4 7 3 0 2 5 1 6)
 (3 1 4 7 5 0 2 6)
 (3 5 0 4 1 7 2 6)
 (5 2 0 7 4 1 3 6)
 (4 2 0 5 7 1 3 6)
 (3 1 7 5 0 2 4 6)
 (5 2 4 6 0 3 1 7)
 (5 3 6 0 2 4 1 7)
 (3 6 4 1 5 0 2 7)
 (4 6 1 5 2 0 3 7))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://community.schemewiki.org/?sicp-ex-2.42"&gt;Better solutions&lt;/a&gt;&lt;/p&gt;
</description><guid isPermaLink="true">http://azeemblog.appspot.com/blog/entry/n-queens-puzzle</guid><pubDate>Wed, 22 Jul 2009 18:05:09 GMT</pubDate></item><item><title>azeemblog v2</title><link>http://azeemblog.appspot.com/blog/entry/azeemblog-v2</link><description>&lt;p&gt;This is my first post in 6 months and the 2nd in this entire blog. Not surprising, if you consider that all of my previous attempts at starting a blog were either dumped right from the start or left to rot in oblivion. Ironically, 'start blogging again' was in the top of my new years resolution this year. To be honest, im really not a blogging type of guy. I dont have or can't find the time either.&lt;/p&gt;

&lt;p&gt;I wasnt really satisfied with the blog script when i first wrote it in January. It felt like big pile of goo. So i re-wrote it. And here it is, azeemblog v2. I hope this inspires me to write more. More on this later.&lt;/p&gt;
</description><guid isPermaLink="true">http://azeemblog.appspot.com/blog/entry/azeemblog-v2</guid><pubDate>Tue, 30 Jun 2009 10:22:28 GMT</pubDate></item><item><title>Hello World</title><link>http://azeemblog.appspot.com/blog/entry/hello-world</link><description>&lt;p&gt;This blog is my little vacation project. &lt;del&gt;Its partially based &lt;a href="http://www.benjamingolub.com/"&gt;Benjamin Golub&lt;/a&gt;'s &lt;a href="http://github.com/bgolub/blog/tree/master"&gt;blog script&lt;/a&gt;.&lt;/del&gt; The script is written entirely in python and hosted on &lt;a href="http://code.google.com/appengine/"&gt;Google's App Engine&lt;/a&gt; framework. Articles are written in &lt;a href="http://daringfireball.net/projects/markdown/syntax"&gt;markdown&lt;/a&gt; and converted to html using the &lt;a href="http://code.google.com/p/python-markdown2/"&gt;markdown2&lt;/a&gt; python library.&lt;/p&gt;

&lt;p&gt;The system is pretty bare bones now, but im still working on stuff. I will upload the source sometime later.&lt;/p&gt;
</description><guid isPermaLink="true">http://azeemblog.appspot.com/blog/entry/hello-world</guid><pubDate>Wed, 07 Jan 2009 06:26:59 GMT</pubDate></item></channel></rss>

