{"id":260,"date":"2012-07-29T00:08:28","date_gmt":"2012-07-28T14:08:28","guid":{"rendered":"http:\/\/juliangamble.com\/blog\/?p=260"},"modified":"2013-12-27T09:47:56","modified_gmt":"2013-12-26T22:47:56","slug":"the-little-schemer-in-clojure-chapter-2","status":"publish","type":"post","link":"https:\/\/juliangamble.com\/blog\/2012\/07\/29\/the-little-schemer-in-clojure-chapter-2\/","title":{"rendered":"The Little Schemer in Clojure &#8211; Chapter 2 &#8211; Do It, Do It Again"},"content":{"rendered":"<p><em>This is the second Chapter of a series of posts about porting\u00a0<a href=\"http:\/\/www.ccs.neu.edu\/home\/matthias\/BTLS\/\">The Little Schemer<\/a>\u00a0to Clojure. You may wish to\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/07\/20\/the-little-schemer-in-clojure\/\">read the intro<\/a>.<\/em><\/p>\n<p>This chapter introduces functions &#8211; and the idea of the list of atoms and the <code>member<\/code> function.<\/p>\n<p>When looking at list atoms, known as a <code>'lat'<\/code> we are introduced to writing functions in Scheme. We write the <code>lat?<\/code> function in Clojure to test for a non-nested list of atoms.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def lat?\r\n  (fn &#x5B;l]\r\n    (cond\r\n      (null? l) true\r\n      (and (seq? l)\r\n           (atom? (first l)))\r\n             (lat? (rest l))\r\n      true false)))\r\n<\/pre>\n<p>There are a couple of things we&#8217;ve transposed here. The key one to raise is the usage of <code>def... fn[]<\/code> rather than <code>defn<\/code>. This is a convention in the book, and glues in the readers minds that functions are by default anonymous, and we give them labels after the fact. This enables the reader to get into the feel of passing anonymous functions around later.<\/p>\n<p>The other thing that is introduced here is the application of the Ten Commandments (of The Little Schemer). The big ideas of the 10 Commandments is to help you solve problems recursively. The first one states that you should a<em>lways ask <code>null?<\/code> \u00a0as the first question in expressing any function<\/em>. In idiomatic Clojure we would use the <code>empty?<\/code> function for this &#8211; but for the sake of being closer to the book, we will not apply it in this function, but use <code>null?<\/code>.<\/p>\n<p>The question behind this is\u00a0<em>what is a non-nested list of atoms and why do we care<\/em>? \u00a0One of the big themes in this book is solving problems using recursion rather than iteration. The point is that before you can solve a problem using recursion (where you&#8217;ve got a nested list) you need to learn to solve it for the base case where there is no nested list. To validate the base case &#8211; we use the <code>lat?<\/code> function.<\/p>\n<p>The next function we look at is the <code>member?<\/code> function.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def member?\r\n  (fn &#x5B;a lat]\r\n    (cond\r\n      (null? lat) false\r\n      true (or\r\n        (= (first lat) a)\r\n        (member? a (rest lat)))) ))\r\n<\/pre>\n<p>Why do we have this function? It&#8217;s the first useful function in the book. We also keep building our recursion skills, and it is a key building block for later functions.<\/p>\n<p><em>You can <a href=\"http:\/\/ideone.com\/y3lYz\">see it working here<\/a>.\u00a0<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second Chapter of a series of posts about porting\u00a0The Little Schemer\u00a0to Clojure. You may wish to\u00a0read the intro. This chapter introduces functions &#8211; and the idea of the list of atoms and the member function. When looking &hellip; <a href=\"https:\/\/juliangamble.com\/blog\/2012\/07\/29\/the-little-schemer-in-clojure-chapter-2\/\">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-260","post","type-post","status-publish","format-standard","hentry","category-clojure","category-thelittleschemer"],"_links":{"self":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/260","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=260"}],"version-history":[{"count":15,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions\/566"}],"wp:attachment":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}