{"id":263,"date":"2012-08-03T21:59:18","date_gmt":"2012-08-03T11:59:18","guid":{"rendered":"http:\/\/juliangamble.com\/blog\/?p=263"},"modified":"2013-12-27T09:44:21","modified_gmt":"2013-12-26T22:44:21","slug":"the-little-schemer-in-clojure-chapter-3","status":"publish","type":"post","link":"https:\/\/juliangamble.com\/blog\/2012\/08\/03\/the-little-schemer-in-clojure-chapter-3\/","title":{"rendered":"The Little Schemer in Clojure &#8211; Chapter 3 &#8211; Cons the Magnificent"},"content":{"rendered":"<p><em>This is the third 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>The first function we look at is <code>rember<\/code>.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def rember\r\n  (fn &#x5B; a lat]\r\n    (cond\r\n      (null? lat) '()\r\n      true (cond\r\n        (= (first lat) a) (rest lat)\r\n        true (cons (first lat)\r\n          (rember\r\n            a (rest lat)))))))\r\n\r\n(println (rember 'banana '(apple banana orange)))\r\n<\/pre>\n<p>The big idea here is list construction. We&#8217;re applying the second commandment of the Little Lisper &#8211; <em>use cons to build lists<\/em> [sequences].<\/p>\n<p>The next function we look at is <code>firsts<\/code>.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def firsts\r\n  (fn &#x5B;l]\r\n    (cond\r\n      (null? l) '()\r\n      true (cons (first (first l))\r\n        (firsts (rest l))))))\r\n\r\n(println (firsts '((large burger)(fries coke)(chocolate sundae))))\r\n<\/pre>\n<p>Here we also meet the Third Commandment of TLS. This states <em>when building a list, describe the typical element, and then cons it onto the natural recursion<\/em>. This goes back to the discussion on <code>lat?<\/code> we were having in the previous chapter. \u00a0You have to identify the base case, and then build on it.<\/p>\n<p><em>You can\u00a0<a href=\"http:\/\/ideone.com\/h6nYz\">see it working here<\/a>.\u00a0<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third Chapter of a series of posts about porting\u00a0The Little Schemer\u00a0to Clojure. You may wish to\u00a0read the intro. The first function we look at is rember. (def rember (fn &#x5B; a lat] (cond (null? lat) &#8216;() true &hellip; <a href=\"https:\/\/juliangamble.com\/blog\/2012\/08\/03\/the-little-schemer-in-clojure-chapter-3\/\">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-263","post","type-post","status-publish","format-standard","hentry","category-clojure","category-thelittleschemer"],"_links":{"self":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/263","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=263"}],"version-history":[{"count":13,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}