{"id":398,"date":"2012-08-31T22:45:33","date_gmt":"2012-08-31T12:45:33","guid":{"rendered":"http:\/\/juliangamble.com\/blog\/?p=398"},"modified":"2013-12-27T10:41:33","modified_gmt":"2013-12-26T23:41:33","slug":"the-little-schemer-in-clojure-chapter-7-shadows","status":"publish","type":"post","link":"https:\/\/juliangamble.com\/blog\/2012\/08\/31\/the-little-schemer-in-clojure-chapter-7-shadows\/","title":{"rendered":"The Little Schemer in Clojure \u2013 Chapter 7 \u2013 Shadows"},"content":{"rendered":"<p><em>This is the seventh 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>So far we&#8217;ve covered\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/07\/20\/the-little-schemer-in-clojure-chapter-1\/\">flat data structures<\/a>,\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/07\/29\/the-little-schemer-in-clojure-chapter-2\/\">reading flat data structures<\/a>,\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/08\/03\/the-little-schemer-in-clojure-chapter-3\/\">creating data structures<\/a>,\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/08\/10\/the-little-schemer-in-clojure-chapter-4-numbers-games\/\">creating a numeric tower<\/a>, working with\u00a0<a href=\"http:\/\/juliangamble.com\/blog\/2012\/08\/17\/the-little-schemer-in-clojure-chapter-5-the-multichapter-chapter\/\">multiple occurrences of a match in the list<\/a>, and <a href=\"http:\/\/juliangamble.com\/blog\/2012\/08\/19\/the-little-schemer-in-clojure-chapter-6-oh-my-gawd-its-full-of-stars\/\">changing our functions to work with nested lists<\/a>.<\/p>\n<p>In this chapter we&#8217;re driving towards our metacircular interpreter by starting to look at evaluating numerical values. So we&#8217;ll look at building an expression, breaking it down into operators and operands, and then handling it and returning a result. So let&#8217;s get started!<\/p>\n<p>First we&#8217;re going to test with a test function to see if the S-expression passed in is a valid numeric expression that we can evaluate. We&#8217;re going to call this function <code>numbered<\/code>. But first we&#8217;ll expand Clojure&#8217;s definition of <code>number?<\/code> &#8211; so it can return a null for an invalid expression as we expect<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n;we need to define number_? to handle true\/false\r\n(def number_?\r\n  (fn &#x5B;a]\r\n    (cond\r\n      (null? a) false\r\n      (number? a) true\r\n      true false)))\r\n\r\n(println (number_? 3))\r\n;\/\/=&gt; true\r\n(println (number_? 'a))\r\n;\/\/=&gt; false\r\n<\/pre>\n<p>So now let&#8217;s do <code>numbered<\/code> to test for S-expressions that we can evaluate as a numeric expression.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def numbered?\r\n  (fn &#x5B;aexp]\r\n    (cond\r\n      (atom? aexp) (number_? aexp)\r\n      (= (first (rest aexp)) '+)\r\n         (and\r\n           (numbered? (first aexp))\r\n           (numbered? (first (rest (rest aexp)))))\r\n      (= (first (rest aexp)) '*)\r\n         (and\r\n           (numbered? (first aexp))\r\n           (numbered? (first (rest (rest aexp)))))\r\n      (= (first (rest aexp)) 'exp)\r\n         (and\r\n           (numbered? (first aexp))\r\n           (numbered? (first (rest (rest aexp)))))\r\n      true false)))\r\n\r\n(println (numbered? '(a b c)))\r\n;\/\/=&gt; false\r\n(println (numbered? '(3 + (4 * 5))))\r\n;\/\/=&gt; true\r\n(println (numbered? '(3 a (4 * 5))))\r\n;\/\/=&gt; false\r\n<\/pre>\n<p>Ok &#8211; so can we simplify numbered? Yes:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def numbered?\r\n  (fn &#x5B;aexp]\r\n    (cond\r\n      (atom? aexp) (number_? aexp)\r\n      true (and\r\n             (numbered? (first aexp))\r\n             (numbered? (first (rest (rest aexp))))))))\r\n\r\n(println (numbered? '(a b c)))\r\n;\/\/=&gt; false\r\n(println (numbered? '(3 + (4 * 5))))\r\n;\/\/=&gt; true\r\n(println (numbered? '(3 a (4 * 5))))\r\n;\/\/=&gt; true\r\n<\/pre>\n<p>Hmm &#8211; notice that the final expression became true in this version, but was false in the previous? That&#8217;s an assumption we&#8217;re making with this new version &#8211; that we know already this is an arithmetic expression. One might think this defeats the purpose of this function. I digress.<\/p>\n<p>Now we&#8217;ll look at the <strong>Eight Commandment<\/strong>. This states that you <em>recur on all subparts that are of the same nature including<\/em>:<br \/>\n&#8211; On all sublists of a list<br \/>\n&#8211; On all the subexpressions of a representation of an arithmetic expression.<br \/>\nThe big idea is that we&#8217;re starting to make a distinction between code and data. We&#8217;re saying that data functions recur on data, and code functions recur on code.<\/p>\n<p>Now you&#8217;ll remember the infamous function <code>eval<\/code> from many languages &#8211; especially LISP. We&#8217;ll start writing the first version of <code>eval<\/code> for our metacircular interpreter starting with a basic version that just looks at arithmetic expressions. We&#8217;ll call it <code>value<\/code>:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(use 'clojure.math.numeric-tower)\r\n; note use of new primitive expt\r\n\r\n(def value\r\n  (fn &#x5B;aexp]\r\n    (cond\r\n      (number_? aexp) aexp\r\n      (= (first (rest aexp)) '+) (+ (value (first aexp)) (value (first (rest (rest aexp)))))\r\n      (= (first (rest aexp)) '*) (* (value (first aexp)) (value (first (rest (rest aexp)))))\r\n      (= (first (rest aexp)) 'exp) (expt (value (first aexp)) (value (first (rest (rest aexp))))))))\r\n\r\n(println (value '(1 + 1)))\r\n;\/\/=&gt; 2\r\n(println (value '(2 + 2)))\r\n;\/\/=&gt; 4\r\n(println (value '(3 exp 3)))\r\n;\/\/=&gt; 27\r\n(println (value '(4 b 4)))\r\n;\/\/=&gt; nil\r\n<\/pre>\n<p>Now we&#8217;re going to take the function above and move from prefix to infix &#8211; just to illustrate how trivial the distinction is from an implementation point of view<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def value\r\n  (fn &#x5B;aexp]\r\n    (cond\r\n      (number_? aexp) aexp\r\n      (= (first  aexp) '+) (+ (value (rest aexp)) (value  (rest (rest aexp))))\r\n      (= (first (rest aexp)) '*) (* (value (first aexp)) (value (first (rest (rest aexp)))))\r\n      (= (first (rest aexp)) 'exp) (expt (value (first aexp)) (value (first (rest (rest aexp))))))))\r\n<\/pre>\n<p>Now in my view that was a little silly. The book included a non-working version of value just to illustrate a violation of the Eighth Commandment. Let&#8217;s get on with it.<\/p>\n<p>Now we&#8217;ll extract out helper functions to get the subexpressions and the operator<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def first-sub-exp\r\n  (fn &#x5B;aexp]\r\n    (first (rest aexp))))\r\n\r\n(def second-sub-exp\r\n  (fn &#x5B;aexp]\r\n    (first (rest (rest aexp)))))\r\n\r\n(def operator\r\n  (fn &#x5B;aexp]\r\n    (first aexp)))\r\n\r\n(def value\r\n  (fn &#x5B;aexp]\r\n    (cond\r\n      (number_? aexp) aexp\r\n      (= (operator aexp) '+) (+ (value (first-sub-exp aexp)) (value  (second-sub-exp aexp)))\r\n      (= (operator aexp) '*) (* (value (first-sub-exp aexp)) (value (second-sub-exp aexp)))\r\n      (= (operator aexp) 'exp) (expt (value (first-sub-exp aexp)) (value (second-sub-exp aexp))))))\r\n\r\n(println (value '(+ 1 1)))\r\n;\/\/=&gt; 2\r\n(println (value '(* 2 2)))\r\n;\/\/=&gt; 4\r\n(println (value '(exp 3 3)))\r\n;\/\/=&gt; 27\r\n(println (value '(b 4 4)))\r\n;\/\/=&gt; nil\r\n<\/pre>\n<p>Cool &#8211; now &#8211; just to illustrate the power of abstraction by functions &#8211; we&#8217;ll switch back from prefix to infix functions.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def first-sub-exp\r\n  (fn &#x5B;aexp]\r\n    (first  aexp)))\r\n\r\n(def operator\r\n  (fn &#x5B;aexp]\r\n    (first (rest aexp))))\r\n\r\n(println (value '(1 + 1)))\r\n;\/\/=&gt; 2\r\n(println (value '(2 * 2)))\r\n;\/\/=&gt; 4\r\n(println (value '(3 exp 3)))\r\n;\/\/=&gt; 27\r\n(println (value '(4 b 4)))\r\n<\/pre>\n<p>Time for another <strong>Commandment<\/strong> &#8211; The Ninth Commandment States <em>Use helper functions to abstract from representations<\/em>. Part of this is the java concept of eclipse&#8217;s extract method. Part of it is the power this gives you to change the function across the whole application and have it impact many parts of the system &#8211; ie a wide-ranging refactor.<\/p>\n<p>Now we&#8217;re testing for <code>null?<\/code>. Although we had to write our own helper method for this in Chapter 1 &#8211; now as part of implementing the metacircular interpreter &#8211; we&#8217;re re-implementing it in terms of other functions so keep our api minimal.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n;This is what we had\r\n(def null?\r\n  (fn &#x5B;a]\r\n    (or\r\n      (nil? a)\r\n      (= () a))))\r\n\r\n;This is what they're proposing\r\n(def null?\r\n  (fn &#x5B;s]\r\n    (and\r\n      (atom? s)\r\n      (= s '()))))\r\n\r\n<\/pre>\n<p>Now it turns out this fails the conversion from Scheme to Clojure. We get the big idea. We&#8217;ll just move on.<\/p>\n<p>Now we&#8217;re going to build our own numeric system out of parenthesis &#8211; again, just to make the implementation of our metacircular interpreter more pure and easier to implement. This is significant because we&#8217;re going to build up our own number representation out of parentheses &#8211; admittedly a strange exercise &#8211; but going to prove that we can do maths in a lisp implemented in itself.<\/p>\n<p>We&#8217;ll start with <code>zero?<\/code>:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def zero_?\r\n  (fn &#x5B;n]\r\n    (null? n)))\r\n\r\n(println (zero_? '()))\r\n;\/\/=&gt;true\r\n(println (zero_? 0))\r\n;=&gt;false\r\n<\/pre>\n<p>Ok &#8211; maybe that didn&#8217;t make sense &#8211; let&#8217;s give a few examples. So now zero is <code>()<\/code>, one is <code>(())<\/code>, and two is <code>(()())<\/code>.<br \/>\nLet&#8217;s do some operations on our new numeric representation &#8211; we&#8217;ll start with a new definition of <code>add1<\/code>.<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def add1\r\n  (fn &#x5B;n]\r\n    (cons '() n)))\r\n\r\n(println (add1 '()))\r\n;\/\/=&gt;(()) ie 1\r\n(println (add1 '(())))\r\n;\/\/=&gt; (() ()) ie 2\r\n<\/pre>\n<p>Now let&#8217;s do <code>sub1<\/code>:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def sub1\r\n  (fn &#x5B;n]\r\n    (rest n)))\r\n\r\n(println (sub1 '(()())))\r\n;\/\/=&gt;(()) ie 1\r\n(println (sub1 '(())))\r\n;\/\/=&gt;() ie 0\r\n(println (sub1 '()))\r\n;\/\/=&gt; () unfortunately with our implementation sub1 of zero returns zero\r\n<\/pre>\n<p>Now we&#8217;ll reimplement our <code>plus<\/code> function:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def +_\r\n  (fn &#x5B;n m]\r\n    (cond\r\n      (zero_? m) n\r\n      true (add1 (+_ n (sub1 m))))))\r\n\r\n(println (+_ '() '()))\r\n;\/\/=&gt; ()\r\n;\/\/  zero plus zero is zero\r\n(println (+_ '(()) '(())))\r\n;\/\/=&gt; (()()) ie 1 plus 1 is 2\r\n<\/pre>\n<p>Now we&#8217;ll test for valid numbers in our new scheme by rewriting our function <code>number?<\/code>:<\/p>\n<pre class=\"brush: clojure; title: ; notranslate\" title=\"\">\r\n(def number_?\r\n  (fn &#x5B;n]\r\n    (cond\r\n      (null? n) true\r\n      true (and\r\n             (null? (first n))\r\n             (number_? (rest n))))))\r\n\r\n(println (number_? '() ))\r\n;\/\/=&gt;true\r\n(println (number_? '(()) ))\r\n;\/\/=&gt;true\r\n(println (number_? '((())) ))\r\n;\/\/=&gt;false\r\n(println (number_? '(()()) ))\r\n;\/\/=&gt;true\r\n<\/pre>\n<p>You can see it running <a href=\"http:\/\/ideone.com\/dmke1\">here<\/a>.<\/p>\n<p><strong>Conclusion:<\/strong><br \/>\nWhy is this chapter called shadows? I haven&#8217;t figured that out. Perhaps it is a shadow of things to come.<\/p>\n<p>Here we&#8217;ve started on the journey of the metacircular interpreter by writing an evaluator for arithmetic expressions. The only primitive we added was <code>expt<\/code>.<\/p>\n<p>In total our primitives so far are: <code>atom?<\/code>, <code>null?<\/code>, <code>first<\/code>, <code>rest<\/code>, <code>cond<\/code>, <code>fn<\/code>, <code>def<\/code>, <code>empty?<\/code>,<code>=<\/code>, <code>cons<\/code>, <code>add1,<\/code>, <code>sub1<\/code>, <code>one?<\/code> and <code>expt<\/code>. These are all the functions (and those in the chapters to come) that we\u2019ll need to implement to get our metacircular interpreter working.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the seventh Chapter of a series of posts about porting\u00a0The Little Schemer\u00a0to Clojure. You may wish to\u00a0read the intro. So far we&#8217;ve covered\u00a0flat data structures,\u00a0reading flat data structures,\u00a0creating data structures,\u00a0creating a numeric tower, working with\u00a0multiple occurrences of a &hellip; <a href=\"https:\/\/juliangamble.com\/blog\/2012\/08\/31\/the-little-schemer-in-clojure-chapter-7-shadows\/\">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-398","post","type-post","status-publish","format-standard","hentry","category-clojure","category-thelittleschemer"],"_links":{"self":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/398","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=398"}],"version-history":[{"count":11,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":572,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions\/572"}],"wp:attachment":[{"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/juliangamble.com\/blog\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}