{"id":551,"date":"2018-11-27T17:41:59","date_gmt":"2018-11-27T17:41:59","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=551"},"modified":"2018-11-27T17:41:59","modified_gmt":"2018-11-27T17:41:59","slug":"recursion-and-lambda-calculus","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/recursion-and-lambda-calculus\/","title":{"rendered":"Recursion and Lambda Calculus"},"content":{"rendered":"<p>So far we haven&#8217;t used names that much for our\u00a0\u03bb calculus functions, mainly just with functions such as sqr. Recursion does require names, because we will call the functions multiple times. Here&#8217;s our function:<\/p>\n<pre>fact: (\u03bbn. (if (= n 1)\u00a0\r\n          1\r\n          (* n (fact (- n 1))))<\/pre>\n<p>Let&#8217;s call (fact 3) and do a\u00a0<span id=\"MathJax-Element-1-Frame\" class=\"MathJax\"><span id=\"MathJax-Span-1\" class=\"math\"><span id=\"MathJax-Span-2\" class=\"mrow\"><span id=\"MathJax-Span-3\" class=\"mi\">\u03b2<\/span><\/span><\/span><\/span>\u00a0reduction on 3:<\/p>\n<pre>(if (= 3 1) 1 (* 3 (fact (- 3 1)))<\/pre>\n<p>Clearly, (= 3 1) returns false, so we&#8217;re left with:<\/p>\n<pre>(* 3 (fact (- 3 1)))<\/pre>\n<p>Which becomes<\/p>\n<pre>(* 3 (fact 2))<\/pre>\n<p>Let&#8217;s do a\u00a0<span id=\"MathJax-Element-1-Frame\" class=\"MathJax\"><span id=\"MathJax-Span-1\" class=\"math\"><span id=\"MathJax-Span-2\" class=\"mrow\"><span id=\"MathJax-Span-3\" class=\"mi\">\u03b2<\/span><\/span><\/span><\/span>\u00a0reduction on 2:<\/p>\n<pre>(* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1))))<\/pre>\n<p>Okay, now this is starting to get complicated. It&#8217;s the same call we just had with (fact 3), but the expression is larger because we have the (* 3 &#8230; part too. Our test (= 2 1) returns false, so we just use the else part of the if to give us<\/p>\n<pre>(* 3 (* 2 (fact (- 2 1))))<\/pre>\n<p>We can reduce (- 2 1) to get<\/p>\n<pre>(* 3 (* 2 (fact 1)))<\/pre>\n<p>Now we do yet another\u00a0\u00a0<span id=\"MathJax-Element-1-Frame\" class=\"MathJax\"><span id=\"MathJax-Span-1\" class=\"math\"><span id=\"MathJax-Span-2\" class=\"mrow\"><span id=\"MathJax-Span-3\" class=\"mi\">\u03b2<\/span><\/span><\/span><\/span>\u00a0reduction to get:<\/p>\n<pre>(* 3 (* 2(if (= 1 1) 1 (* 1 (fact (- 1 1)))))<\/pre>\n<p>This time the if will return true, because (= 1 1) evaluates to true, so we have our simple case, which gives us:<\/p>\n<pre>(* 3 (* 2 1))<\/pre>\n<p>Suddenly this looks much simpler!<\/p>\n<p>How about with ASTs?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far we haven&#8217;t used names that much for our\u00a0\u03bb calculus functions, mainly just with functions such as sqr. Recursion does require names, because we will call the functions multiple times. Here&#8217;s our function: fact: (\u03bbn. (if (= n 1)\u00a0 <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/recursion-and-lambda-calculus\/\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-551","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":1,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/551\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/551\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}