{"id":525,"date":"2018-11-18T18:06:46","date_gmt":"2018-11-18T18:06:46","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=525"},"modified":"2018-11-20T08:36:56","modified_gmt":"2018-11-20T08:36:56","slug":"logic-and-%ce%bb-calculus","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/logic-and-%ce%bb-calculus\/","title":{"rendered":"Logic and\u00a0\u00a0\u03bb calculus"},"content":{"rendered":"<p>Let&#8217;s run through some simple Boolean expressions and their associated ASTs.<\/p>\n<p><strong>(or #t #f)<\/strong> evaluates to <strong>#t.<\/strong><\/p>\n<div id=\"attachment_529\" style=\"width: 198px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-529\" class=\" wp-image-529\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or.png\" alt=\"\" width=\"188\" height=\"132\" \/><p id=\"caption-attachment-529\" class=\"wp-caption-text\">An AST with the or operator<\/p><\/div>\n<p>Similarly,\u00a0<strong>(not #t)<\/strong> is<strong> #f<\/strong>, which is fair, after all, something that&#8217;s not true has to be false. Play around with these in Racket to get a feel for them, as they can be arbitrarily complex.<\/p>\n<p>Here is the AST for\u00a0<strong>(or (and #t #f) (or #t #f))<\/strong><\/p>\n<div id=\"attachment_531\" style=\"width: 364px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-531\" class=\" wp-image-531\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-2.png\" alt=\"\" width=\"354\" height=\"220\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-2.png 566w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-2-300x187.png 300w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><p id=\"caption-attachment-531\" class=\"wp-caption-text\">An AST with multiple levels of Boolean operators.<\/p><\/div>\n<p>Then we get:<\/p>\n<div id=\"attachment_530\" style=\"width: 361px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-530\" class=\"wp-image-530 size-full\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-3.png\" alt=\"\" width=\"351\" height=\"186\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-3.png 351w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/or-3-300x159.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><p id=\"caption-attachment-530\" class=\"wp-caption-text\">The AST with the lowest level resolved<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>What happens if we pass numbers to these operators? In\u00a0\u03bb calculus and Racket,\u00a0<strong>all numbers are considered true<\/strong>. Note that this might be different from what you may be used to in other programming languages, for example, in some languages, -1 or 0 are considered false.<\/p>\n<p>Let&#8217;s run through some examples, they are written in Racket, but are the same in\u00a0\u03bb calculus.<\/p>\n<pre>&gt; (if 1 \"first\" \"second\")\r\n\"first\"\r\n&gt; (or 1 0)\r\n1\r\n&gt; (or 0 1)\r\n0\r\n&gt; (or 31 #t)\r\n31\r\n&gt; (or #t 31)\r\n#t\r\n<\/pre>\n<p>So what exactly is going on here? If all numbers are considered to be true, shouldn&#8217;t\u00a0<strong>or<\/strong>\u00a0return true? As far as it is concerned, it\u00a0<strong>is<\/strong> returning true, because it is returning a number, so now the question becomes how does it know which number to return? The rule for\u00a0<strong>or<\/strong> is that it returns the first TRUE value it can find, otherwise it returns FALSE. The fact that sometimes that first TRUE value is not a Boolean doesn&#8217;t matter,\u00a0<strong>or\u00a0<\/strong>will happily return whatever it is given.<\/p>\n<p>What about\u00a0<strong>and<\/strong>? It basically does the opposite of\u00a0<strong>or<\/strong> and returns the last TRUE value it can find, otherwise it returns FALSE. Look at these examples:<\/p>\n<pre>&gt; (and 3 -1)\r\n-1\r\n&gt; (and -1 3)\r\n3\r\n&gt; (and 1 #f)\r\n#f\r\n&gt; (and #f 2)\r\n#f\r\n<\/pre>\n<p>This means that for\u00a0<strong>and\u00a0<\/strong>to return TRUE, it must test every value given to it, while for\u00a0<strong>or<\/strong> to return FALSE, it must evaluate everything.\u00a0 You might guess from the way we described these that operators such as\u00a0<strong>or\u00a0<\/strong>and\u00a0<strong>and<\/strong> can take any number of arguments, just like other prefix operators, and this is exactly true, and we can apply the exact same rules we did when there were two arguments.<\/p>\n<p>What about\u00a0<strong>and<\/strong>? It basically does the opposite of\u00a0<strong>or<\/strong> and returns the last TRUE value it can find, otherwise it returns FALSE. Look at these examples:<\/p>\n<pre>&gt; (and 1 2 3 4)\r\n4\r\n&gt; (or 1 2 3 4)\r\n1\r\n&gt; (not 1 2 3 4)\r\n<strong>not: <\/strong>arity mismatch<\/pre>\n<p>Okay, so they don&#8217;t all take multiple arguments; specifically,\u00a0<strong>not<\/strong> is only defined for a single argument, and that&#8217;s fair enough, given that it simply inverts the truth of what it is given.<\/p>\n<p>We&#8217;ve seen strings floating around in some of these examples, typically as return values. However, with Functional Programming languages, whatever one function returns another one could take as a parameter, so we need to understand the implication of strings being passed to Boolean operators too. As with numbers, all strings are true, and the operators all work the same way.<\/p>\n<pre>&gt; (and \"hello\" \"goodbye\")\r\n\"goodbye\"\r\n&gt; (or \"hello\" \"goodbye\")\r\n\"hello\"\r\n<\/pre>\n<p>In the next section we&#8217;ll look at how to use these <a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/using-conditionals-in-functions\/\">conditionals in functions<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s run through some simple Boolean expressions and their associated ASTs. (or #t #f) evaluates to #t. Similarly,\u00a0(not #t) is #f, which is fair, after all, something that&#8217;s not true has to be false. Play around with these in Racket <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/logic-and-%ce%bb-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-525","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/525","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=525"}],"version-history":[{"count":5,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/525\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/525\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}