{"id":1168,"date":"2010-03-14T03:14:24","date_gmt":"2010-03-14T09:14:24","guid":{"rendered":"http:\/\/bililite.nfshost.com\/blog\/?p=1168"},"modified":"2010-03-16T17:31:32","modified_gmt":"2010-03-16T23:31:32","slug":"learning-scheme","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2010\/03\/14\/learning-scheme\/","title":{"rendered":"Learning Scheme"},"content":{"rendered":"<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>\r\n<!--more--><p>Problem 1:\r\n<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.\r\n\r\nFind the sum of all the multiples of 3 or 5 below 1000.<\/blockquote><\/p>\r\n<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>\r\n<pre><code class=\"language-lisp\">(define divides\r\n  (lambda (a b)\r\n    (= 0 (remainder b a))))\r\n\r\n(define euler1\r\n  (lambda (n)\r\n    (cond\r\n      ((= n 1) \r\n        0)\r\n      ((or (divides 5 n) (divides 3 n))\r\n        (+ n (euler1 (- n 1))))\r\n      (else (euler1 (- n 1))))))\r\n\r\n(euler1 999)<\/code><\/pre>\r\n<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>\r\n<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\r\n5+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>","protected":false},"excerpt":{"rendered":"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.","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/1168"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=1168"}],"version-history":[{"count":8,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/1168\/revisions"}],"predecessor-version":[{"id":1178,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/1168\/revisions\/1178"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}