12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!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">
- <head>
- <title>Booleans</title>
- <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
- <link rel="stylesheet" type="text/css" href="kawa.css"/>
- <script src="kawa-ebook.js" type="text/javascript"/>
- <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
- <link rel="prev" href="tutorial-Introduction.xhtml" title="Introduction"/>
- <link rel="next" href="tutorial-Numbers.xhtml" title="Numbers"/>
- </head>
- <body>
- <header/>
- <section class="sect1" title="Booleans" epub:type="subchapter" id="Tutorial---Booleans">
- <div class="titlepage">
- <div>
- <div>
- <h2 class="title" style="clear: both">Booleans</h2>
- </div>
- </div>
- </div>
- <p>Scheme uses the syntax <code class="literal">#t</code> and <code class="literal">#f</code>
- for Boolean true and false value, respectively. For example, the
- “less-than” function is named <code class="literal"><</code>.
- Its result is true if the first argument is less than the second (or, if
- there are more than two arguments, that they are in increasing order):
- </p>
- <pre class="screen">(< 3 4) ⇒ #t
- (< -3 -4) ⇒ #f
- (< 2 3 5 7 11)) ⇒ #t
- </pre>
- <p>The <code class="literal">if</code> special form takes two or three sub-expressions:
- It evaluates the first expression.
- If that is true it evaluates the second expression;
- otherwise it evaluates the third expression, if provided:
- </p>
- <pre class="screen">(if (< 3 4) (+ 5 5) (+ 5 6)) ⇒ 10
- </pre>
- <p>We call <code class="literal">if</code> a special form rather than a function,
- because for a function all the arguments are evaluated before the
- function is called, but in a special form that is not neceassarily the case.
- </p>
- <p>In addition to <code class="literal">#t</code> any value except <code class="literal">#f</code>
- counts as “true” when evaluating the first expression of an <code class="literal">if</code>:
- </p>
- <pre class="screen">(if 0 (+ 5 5) (+ 5 6)) ⇒ 11
- </pre>
- <p>You can use <code class="literal">and</code>, <code class="literal">or</code>,
- and <code class="literal">not</code> to create complex boolean expressions.
- Of these <code class="literal">and</code> and <code class="literal">or</code>
- are special forms that only evaluate as many of the sub-expressions as needed.
- </p>
- <pre class="screen">(if (not (and (>= i 0) (<= i 9)))
- (display "error"))
- </pre>
- <p>You can use the <code class="literal">cond</code> form as an alternative to
- <code class="literal">if</code>:
- </p>
- <pre class="screen">(cond ((< 3 3) 'greater)
- ((> 3 3) 'less)
- (else ’equal)) ⇒ equal
- </pre>
- <p>The null value (written as <code class="literal">#!null</code> in Kawa or <code class="literal">null</code> in Java)
- is also considered as false.
- </p>
- </section>
- <footer>
- <div class="navfooter">
- <p>
- Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
- <p>
- Previous: <a accesskey="p" href="tutorial-Introduction.xhtml">Introduction</a></p>
- <p>
- Next: <a accesskey="n" href="tutorial-Numbers.xhtml">Numbers</a></p>
- </div>
- </footer>
- </body>
- </html>
|