tutorial-Booleans.xhtml 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Booleans</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="tutorial-Introduction.xhtml" title="Introduction"/>
  10. <link rel="next" href="tutorial-Numbers.xhtml" title="Numbers"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Booleans" epub:type="subchapter" id="Tutorial---Booleans">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Booleans</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Scheme uses the syntax <code class="literal">#t</code> and <code class="literal">#f</code>
  23. for Boolean true and false value, respectively. For example, the
  24. “less-than” function is named <code class="literal">&lt;</code>.
  25. Its result is true if the first argument is less than the second (or, if
  26. there are more than two arguments, that they are in increasing order):
  27. </p>
  28. <pre class="screen">(&lt; 3 4) ⇒ #t
  29. (&lt; -3 -4) ⇒ #f
  30. (&lt; 2 3 5 7 11)) ⇒ #t
  31. </pre>
  32. <p>The <code class="literal">if</code> special form takes two or three sub-expressions:
  33. It evaluates the first expression.
  34. If that is true it evaluates the second expression;
  35. otherwise it evaluates the third expression, if provided:
  36. </p>
  37. <pre class="screen">(if (&lt; 3 4) (+ 5 5) (+ 5 6)) ⇒ 10
  38. </pre>
  39. <p>We call <code class="literal">if</code> a special form rather than a function,
  40. because for a function all the arguments are evaluated before the
  41. function is called, but in a special form that is not neceassarily the case.
  42. </p>
  43. <p>In addition to <code class="literal">#t</code> any value except <code class="literal">#f</code>
  44. counts as “true” when evaluating the first expression of an <code class="literal">if</code>:
  45. </p>
  46. <pre class="screen">(if 0 (+ 5 5) (+ 5 6)) ⇒ 11
  47. </pre>
  48. <p>You can use <code class="literal">and</code>, <code class="literal">or</code>,
  49. and <code class="literal">not</code> to create complex boolean expressions.
  50. Of these <code class="literal">and</code> and <code class="literal">or</code>
  51. are special forms that only evaluate as many of the sub-expressions as needed.
  52. </p>
  53. <pre class="screen">(if (not (and (&gt;= i 0) (&lt;= i 9)))
  54. (display "error"))
  55. </pre>
  56. <p>You can use the <code class="literal">cond</code> form as an alternative to
  57. <code class="literal">if</code>:
  58. </p>
  59. <pre class="screen">(cond ((&lt; 3 3) 'greater)
  60. ((&gt; 3 3) 'less)
  61. (else ’equal)) ⇒ equal
  62. </pre>
  63. <p>The null value (written as <code class="literal">#!null</code> in Kawa or <code class="literal">null</code> in Java)
  64. is also considered as false.
  65. </p>
  66. </section>
  67. <footer>
  68. <div class="navfooter">
  69. <p>
  70. Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
  71. <p>
  72. Previous: <a accesskey="p" href="tutorial-Introduction.xhtml">Introduction</a></p>
  73. <p>
  74. Next: <a accesskey="n" href="tutorial-Numbers.xhtml">Numbers</a></p>
  75. </div>
  76. </footer>
  77. </body>
  78. </html>