{"id":521,"date":"2018-11-18T15:45:40","date_gmt":"2018-11-18T15:45:40","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=521"},"modified":"2018-11-18T15:47:20","modified_gmt":"2018-11-18T15:47:20","slug":"boolean-and-relational-operators","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/boolean-and-relational-operators\/","title":{"rendered":"Boolean and Relational Operators"},"content":{"rendered":"<p>Boolean operators are operators such as\u00a0<strong>and, or<\/strong> and\u00a0<strong>not,\u00a0<\/strong>and specifically deal with logic values. For example, (TRUE\u00a0<strong>AND\u00a0<\/strong>TRUE) evaluates to TRUE, while (TRUE\u00a0<strong>AND\u00a0<\/strong>FALSE) is FALSE. Relational operators, on the other hand, are\u00a0<em>comparison<\/em> operators, things like\u00a0<strong>&lt;<\/strong>,\u00a0<strong>&gt;,\u00a0<\/strong><strong>=\u00a0<\/strong>etc. These compare two or more items and return a Boolean value to indicate whether or not the relation is TRUE. For example ( 2 &gt; 1) is TRUE, while (1 &gt; 2) is FALSE.<\/p>\n<p>In prefix notation, as you would expect, we simply put the operator first:<\/p>\n<ul>\n<li>(&gt; 2 1)..True<\/li>\n<li>(&lt; 4 2) .. False<\/li>\n<\/ul>\n<p>In Racket, we use #t and #f for TRUE and FALSE, so we could have<\/p>\n<pre>&gt; (&gt; 2 1)\r\n#t\r\n&gt; (= 2 1)\r\n#f\r\n&gt; (&lt; (+ 3 1) (* 4 5))\r\n#t\r\n<\/pre>\n<p>Notice in the third example we have a mix of arithmetic and relational operators. This is okay as long as their order makes sense; in that third example, the arithmetic operations return 4 and 20, respectively, which are perfectly legal to pass to\u00a0<strong>&lt;<\/strong>. However, if we tried this\u00a0<strong>(+ 2 (&gt; 3 1))<\/strong> we would get an error, because\u00a0<strong>+\u00a0<\/strong>is not defined for operating on a Boolean value, that is, (+ 2 #t) doesn&#8217;t really make sense.<\/p>\n<p>Where we want to use these sorts of logic values is in conditionals, specifically in the\u00a0<strong>predicate\u00a0<\/strong>for a conditional, which is the logic test performed in something like an\u00a0<strong>IF\u00a0<\/strong>statement. A general view of a conditional is something like<\/p>\n<p><strong>if E then C1 else C2<\/strong><\/p>\n<p>That is, if the predicate in expression\u00a0<strong>E\u00a0<\/strong> is true, then execute <strong>C1<\/strong>, otherwise execute\u00a0<strong>C2<\/strong>. The\u00a0\u03bb calculus and Racket view of things is similar:<\/p>\n<p><strong>if E then RETURN C1 else RETURN<\/strong> <strong>C2<\/strong><\/p>\n<p>Why do we say RETURN? Because typically we&#8217;re dealing with expressions, so, depending on the result of the test in\u00a0<strong>E,\u00a0<\/strong>we return the value in either\u00a0<strong>C1 <\/strong>or\u00a0<strong>C2.<\/strong><\/p>\n<p>Let&#8217;s look at some examples. Here&#8217;s a conditional in Racket:<\/p>\n<pre>&gt; (if (&gt; 2 0) \"first\" \"second\")\r\n\"first\"\r\n<\/pre>\n<p>That is, if 2 is greater than 0, return the word &#8220;first&#8221;, otherwise return the word &#8220;second&#8221;. Pretty straightforward&#8230; so far!<\/p>\n<p>Here&#8217;s a\u00a0\u03bb calculus expression:<\/p>\n<p><strong>(<\/strong><strong>\u03bbxy. if (&gt; x y) x y)<\/strong><\/p>\n<p>This takes two parameters and returns the first if it is larger, and otherwise returns the second.<\/p>\n<div id=\"attachment_522\" style=\"width: 254px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-522\" class=\" wp-image-522\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-01.png\" alt=\"\" width=\"244\" height=\"209\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-01.png 449w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-01-300x257.png 300w\" sizes=\"auto, (max-width: 244px) 100vw, 244px\" \/><p id=\"caption-attachment-522\" class=\"wp-caption-text\">A simple if construct in lambda calculus. The part in green is the predicate, which returns a logic value of true or false.<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>If we get a true value in the predicate, the AST returns\u00a0<strong>x<\/strong>, otherwise it returns\u00a0<strong>y<\/strong>.<\/p>\n<p>Here&#8217;s a similar but different one:<\/p>\n<p><strong>(<\/strong><strong>\u03bbxy. &gt; x y)<\/strong><\/p>\n<p>Notice that this one doesn&#8217;t even have an\u00a0<strong>IF<\/strong> part, it just performs a logic test and returns the value of that &#8212; either true or false.<\/p>\n<div id=\"attachment_523\" style=\"width: 381px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-523\" class=\"wp-image-523 size-full\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-02.png\" alt=\"\" width=\"371\" height=\"263\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-02.png 371w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Conditional-02-300x213.png 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><p id=\"caption-attachment-523\" class=\"wp-caption-text\">A lambda expression that returns a Boolean value, but which doesn&#8217;t use an if. Notice that it simply returns the result of the &gt; test.<\/p><\/div>\n<p>\u03bb calculus can use all the classical Boolean constructs, such as\u00a0<strong>and, or<\/strong><strong>, not<\/strong> etc. The <a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/logic-and-\u03bb-calculus\/\">next section will look at these<\/a> and highlight some areas that might be surprising to people who are used to programming with other languages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Boolean operators are operators such as\u00a0and, or and\u00a0not,\u00a0and specifically deal with logic values. For example, (TRUE\u00a0AND\u00a0TRUE) evaluates to TRUE, while (TRUE\u00a0AND\u00a0FALSE) is FALSE. Relational operators, on the other hand, are\u00a0comparison operators, things like\u00a0&lt;,\u00a0&gt;,\u00a0=\u00a0etc. These compare two or more items and <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/boolean-and-relational-operators\/\">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-521","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/521","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=521"}],"version-history":[{"count":2,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/521\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/521\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}