<?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/"
	>

<channel>
	<title>Hacking at 0300 &#187; Scheme</title>
	<atom:link href="http://bililite.nfshost.com/blog/category/scheme/feed/" rel="self" type="application/rss+xml" />
	<link>http://bililite.nfshost.com/blog</link>
	<description>Thoughts on web design and programming from a very occasional volunteer webmaster</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:05:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript and Lisp</title>
		<link>http://bililite.nfshost.com/blog/2011/01/05/javascript-and-lisp/</link>
		<comments>http://bililite.nfshost.com/blog/2011/01/05/javascript-and-lisp/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 16:42:33 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://bililite.nfshost.com/blog/?p=1446</guid>
		<description><![CDATA[As Douglas Crockford has said, javascript is Lisp is C's clothing. That is part of what make javascript fun to program, but I never realized how literally true that statement is until I found this fascinating article by Thomas Lord about GNU and Scheme, that claims: I've read, since then, that up to around that [...]]]></description>
			<content:encoded><![CDATA[<p>As Douglas Crockford has said, <a href="http://javascript.crockford.com/javascript.html">javascript is Lisp is C's clothing</a>. That is part of what make javascript fun to program, but I never realized how literally true that statement is until I found <a href="http://basiscraft.com/0800-0100-the-tcl-war.html">this fascinating article by Thomas Lord</a> about GNU and Scheme, that claims:</p>
<blockquote>I've read, since then, that up to around that point Brendan Eich had been working on a Scheme-based extension language for Netscape Navigator. Such was the power of the hegemony of the high level folks at Sun that the word came down on Mr. Eich: "Get it done. And make it look like Java." Staying true to his sense of Self, he quickly knocked out the first implementation of Mocha, later renamed Javascript.</blockquote>
<p>Explains a lot. And shows what an under-appreciated genius Brendan Eich is.</p>

]]></content:encoded>
			<wfw:commentRss>http://bililite.nfshost.com/blog/2011/01/05/javascript-and-lisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Scheme: Euler Problem 3</title>
		<link>http://bililite.nfshost.com/blog/2010/07/13/learning-scheme-euler-problem-3/</link>
		<comments>http://bililite.nfshost.com/blog/2010/07/13/learning-scheme-euler-problem-3/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 02:46:30 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://bililite.nfshost.com/blog/?p=1216</guid>
		<description><![CDATA[Been a long time since I last posted; real life has a way of getting in the way. The past few months read like the back cover of A Series of Unfortunate Events: if you don't want to hear about a thwarted move, a house fire, 2 graduations, negotiations for a new job and a [...]]]></description>
			<content:encoded><![CDATA[<p>Been a long time since I last posted; real life has a way of getting in the way. The past few months read like the back cover of <a href="http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&#038;field-keywords=A+Series+of+Unfortunate+Events&#038;x=0&#038;y=0&#038;tag=bililitecom-20">A Series of Unfortunate Events</a>: if you don't want to hear about a thwarted move, a house fire, 2 graduations, negotiations for a new job and a carrot salad with lots of garlic, then you should put this down and live someone else's life.</p>
<p>But I'm not complaining; everything looks like it's working out well.</p>
<p>Going back to Scheme, the next <a href="http://projecteuler.net/index.php?section=problems">Project Euler</a> problem is:</p>
<blockquote>The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?</blockquote>
<p>And straightforwardly:</p>
<pre><code class="language-lisp">(define gpf ; greatest prime factor
  (lambda (n try) ; n is the number to find the factor of; try is the lowest number to try
    (cond
      ((> try (sqrt n)) n) ; no need to try a factor higher than the square root; if we get here, n is prime
      ((divides try n) (gpf (/ n try) try)) ; divide out the current trial factor and try again
      (else (gpf n (+ try 1))))))
(define euler3
  (lambda (n) (gpf n 2)))</code></pre>
<p><code class="language-lisp">(euler3 600851475143)</code> gives us the answer 6857 pretty quickly.</p>
<p>But this is getting boring; I'm not using the interesting parts of Scheme. This sort of problem cries out for a prime number generator, and that cries out for <a href="http://community.schemewiki.org/?call-with-current-continuation"><code class="language-lisp">call-with-current-continuation</code></a>, and <em>that</em> cries out for some more playing and learning. I'll see what I can do.</p>
]]></content:encoded>
			<wfw:commentRss>http://bililite.nfshost.com/blog/2010/07/13/learning-scheme-euler-problem-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Scheme, Euler problem 2</title>
		<link>http://bililite.nfshost.com/blog/2010/03/16/learning-scheme-euler-problem-2/</link>
		<comments>http://bililite.nfshost.com/blog/2010/03/16/learning-scheme-euler-problem-2/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:47:06 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://bililite.nfshost.com/blog/?p=1177</guid>
		<description><![CDATA[The second project Euler problem is: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence [...]]]></description>
			<content:encoded><![CDATA[<p>The second project Euler problem is:

<blockquote>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.</blockquote>

</p><span id="more-1177"></span>
<p>Brute force Fibonacci generator:</p>
<pre><code class="language-lisp">(define fibo
  (lambda (n)
    (cond
      ((= n 1)
       1)
      ((= n 2)
       2)
      (else (+ (fibo (- n 1)) (fibo (- n 2)))))))</code></pre>
<p>But that takes exponential time, and the problem requires generating all of the numbers anyway, so we can use something like:</p>
<pre><code class="language-lisp">(define fibo-sum
  (lambda (a b lim)
    (let* ((c (+ a b)))
      (cond
        ((< lim c) 0)
        (else (+ c (fibo-sum b c lim)))))))</code></pre>
<p> to get the sum of all the fibonacci numbers less than lim (use <code class="language-lisp">(fibo-sum 1 2 4000000)</code>). I could add a test for even numbers to solve the problem, but more elegantly we notice that the pattern of the fibonacci numbers has to be odd-odd-even-odd-odd-even, so we have:</p>
<pre><code class="language-lisp">(define euler2
  (lambda (a b lim)
    (let* ((c (+ a b))
           (d (+ b c))
           (e (+ c d)))
      (cond
        ((< lim e) 0)
        (else (+ e (euler2 d e lim)))))))
(euler2 1 0 4000000)</code></pre>
<p>We start the fibonacci sequence with 1 0 so that the 2 is part of our sum.  And we get the correct answer, 4613732, imperceptibly fast.</p>
<p>One bug that took me forever to find was a mistyped line:</p>
<pre><code class="language-lisp">(let* ((c (+ a b))
  (d (+ b c))
   (e (+ d e)))
</code></pre>
<p>Where <code>e</code> was defined in terms of <code>d</code> and <code>e</code>, so it used the global value of <code>e</code>. If it hadn't been defined, it would have given me an error immediately, but unfortunately, DrScheme has it pre-defined as <code class="language-lisp">#i2.718281828459045</code> so my code went happily along giving me the wrong answers.</p>

]]></content:encoded>
			<wfw:commentRss>http://bililite.nfshost.com/blog/2010/03/16/learning-scheme-euler-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Scheme</title>
		<link>http://bililite.nfshost.com/blog/2010/03/14/learning-scheme/</link>
		<comments>http://bililite.nfshost.com/blog/2010/03/14/learning-scheme/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 09:14:24 +0000</pubDate>
		<dc:creator>Danny</dc:creator>
				<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://bililite.nfshost.com/blog/?p=1168</guid>
		<description><![CDATA[OK, so my new mini project is to learn Scheme. It can't be too hard, right? I mean Javascript is just Lisp in C's clothing, and I'm good at Javascript. I've installed PLT-Scheme and I figure the best way to learn it is solving Project Euler problems until I get bored. Problem 1: If we [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so my new mini project is to learn <a href="http://download.plt-scheme.org/doc/360/html/t-y-scheme/t-y-scheme.html">Scheme</a>. It can't be too hard, right? I mean <a href="http://javascript.crockford.com/javascript.html">Javascript is just Lisp in C's clothing</a>, and I'm good at Javascript. I've installed <a href="http://www.plt-scheme.org/">PLT-Scheme</a> and I figure the best way to learn it is solving <a href="http://projecteuler.net/">Project Euler</a> problems until I get bored.</p>
<span id="more-1168"></span><p>Problem 1:
<blockquote>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.</blockquote></p>
<p>Translating Javascript to Scheme means <code class="language-javascript">function(x, y) {foo;bar;}</code> becomes <code class="language-lisp">(lambda (x y) (foo) (bar))</code> and <code class="language-javascript">var x = y;</code> becomes <code class="language-lisp">(define x y)</code> and realizing that all loops have to be made into tail-recursive function calls gives us this pretty easily:</p>
<pre><code class="language-lisp">(define divides
  (lambda (a b)
    (= 0 (remainder b a))))

(define euler1
  (lambda (n)
    (cond
      ((= n 1) 
        0)
      ((or (divides 5 n) (divides 3 n))
        (+ n (euler1 (- n 1))))
      (else (euler1 (- n 1))))))

(euler1 999)</code></pre>
<p>And the correct answer is 233168. Not bad for my first Scheme program! (Certainly more interesting than <code class="language-lisp">(display "hello, world")</code>).</p>
<p>Addendum: I just looked at the forum at Project Euler and I see that there's a much better solution; just add the series 3+6+9....999, which is simply 3*(1+2+3+...333) or 3* 333 * (333+1)/2, to the series
5+10+15+...995, or 5 * 199 * (199+1)/2 and subtract the series 15+30+45... which is counted twice. No loops! Less interesting for learning to program, though.</p>]]></content:encoded>
			<wfw:commentRss>http://bililite.nfshost.com/blog/2010/03/14/learning-scheme/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

