{"id":257,"date":"2012-07-20T23:47:44","date_gmt":"2012-07-20T13:47:44","guid":{"rendered":"http:\/\/juliangamble.com\/blog\/?p=257"},"modified":"2013-12-27T09:46:58","modified_gmt":"2013-12-26T22:46:58","slug":"the-little-schemer-in-clojure-chapter-1","status":"publish","type":"post","link":"https:\/\/juliangamble.com\/blog\/2012\/07\/20\/the-little-schemer-in-clojure-chapter-1\/","title":{"rendered":"The Little Schemer in Clojure &#8211;  Chapter 1 &#8211; Toys"},"content":{"rendered":"<p><em>This is the first Chapter of a series of posts about porting <a href=\"http:\/\/www.ccs.neu.edu\/home\/matthias\/BTLS\/\">The Little Schemer<\/a> to Clojure. You may wish to <a href=\"http:\/\/juliangamble.com\/blog\/2012\/07\/20\/the-little-schemer-in-clojure\/\">read the intro<\/a>.<\/em><\/p>\n<p>The big theme in Chapter 1 is <em><strong>Simplicity of Data<\/strong><\/em>. In The Little Schemer world there are two levels in the syntax that describes data &#8211; <em>atoms<\/em> and <em>lists<\/em>. Note that an atom in TLS refers to a a <em>string of characters<\/em> (perhaps you could consider this a list element &#8211; ie a non-list), wheras in Clojure an atom refers to a unit of a transaction.<\/p>\n<p>To test for atoms &#8211; we&#8217;ll use the <code>atom?<\/code> function:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def atom?\r\n  (fn &#x5B;a]\r\n    (not (seq? a))))\r\n<\/pre>\n<p>Note that we&#8217;re not using the <code>defn<\/code> syntax in Clojure but <code>def ... fn<\/code>. This is intentional because the book uses this standard, and then makes use of it later in the book when it passes anonymous functions around.<\/p>\n<div>\n<p>Speaking of <code>null<\/code> &#8211; the <a href=\"http:\/\/hyperpolyglot.org\/lisp\">original Scheme has a different concept of nil<\/a> than <a href=\"http:\/\/clojure.org\/lisps\">what we see in Clojure today<\/a>. To make Clojure act like the <code>null<\/code> tests in the book &#8211; we use the <code>null?<\/code> function:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def null?\r\n  (fn &#x5B;a]\r\n    (or\r\n      (nil? a)\r\n      (= () a))))\r\n<\/pre>\n<p>In TLS the thematic emphasis is also on the\u00a0<em><strong>Simplicity of Syntax<\/strong><\/em>. All data structures are represented by lists, and lists of lists (ie nested listed). In Clojure we have several types of data structures &#8211; but the closest to what we&#8217;re dealing with here is the <a href=\"http:\/\/clojure.org\/sequences\">Sequence<\/a>. \u00a0(<em>Now it&#8217;s more complex than that &#8211; because in Clojure a sequence is more like an iterator interface &#8211; but we&#8217;ll start there<\/em>.)<\/p>\n<p>The other concept to get in Chapter 1 is that lists are treated as stacks rather than arrays. We push elements on the end and pop them off. Our primitives to do this in TLS are <code>cons<\/code> to push, <code>car<\/code> to pop and <code>cdr<\/code> to get the list remainder. In Clojure this corresponds to <code>cons<\/code> to push, <code>first<\/code> to pop and <code>rest<\/code> to get the sequence remainder. (<em>Note that Clojure also has <code>conj<\/code>, but we&#8217;ll stick to sequences and using cons for now)<\/em><\/p>\n<p>The last trick that Chapter 1 introduces are nested lists. The short summary of this is that if you push a list onto a list you get a nested list (using <code>cons<\/code>). To reverse this then you pop the nested list off the list (using <code>car\/first<\/code>).<\/p>\n<div><em>You can <a href=\"http:\/\/ideone.com\/7TnNN\">see it working here<\/a>.<\/em><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is the first Chapter of a series of posts about porting The Little Schemer to Clojure. You may wish to read the intro. The big theme in Chapter 1 is Simplicity of Data. In The Little Schemer world there &hellip; <a href=\"https:\/\/juliangamble.com\/blog\/2012\/07\/20\/the-little-schemer-in-clojure-chapter-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[3,12],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-clojure","category-thelittleschemer"],"_links":{"self":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":23,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/257\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}