tutorial-Introduction.xhtml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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>Introduction</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-index.xhtml" title="Kawa Scheme Tutorial"/>
  10. <link rel="next" href="tutorial-Booleans.xhtml" title="Booleans"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Introduction" epub:type="subchapter" id="Tutorial---Introduction">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Introduction</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>You’ve heard about all the hot scripting languages
  23. – you might even be tired of hearing about them.
  24. But Kawa offers you something different than the
  25. scripting-language <span class="emphasis"><em>du-jour</em></span> can.
  26. You may be interested in one that runs on the Java virtual machine,
  27. either because you have to interact with other Java tools,
  28. or because you like having access to all the Java packages out there.
  29. Or maybe you don’t care about Java, but you care about performance.
  30. If so, let me tell you about Kawa, which is actually one of the
  31. very oldest language implementations running on the Java Virtual Machine,
  32. dating back to 1996.
  33. </p>
  34. <p>The Kawa language is a dialect/implementation of the Scheme language.
  35. (The Kawa project also supports other languages, including
  36. <a class="ulink" href="http://www.w3.org/XML/Query" target="_top">XQuery</a>
  37. and <a class="ulink" href="http://jemacs.sourceforge.net" target="_top">Emacs Lisp</a>,
  38. as well as tools for implementing mew programming languages,
  39. but we won’t cover that in this tutorial.)
  40. </p>
  41. <p><a class="ulink" href="http://www.schemers.org/" target="_top">Scheme</a>
  42. is an established language with many
  43. <a class="ulink" href="http://community.schemewiki.org/?scheme-faq-standards#implementations" target="_top">implementations</a>,
  44. a <a class="ulink" href="http://www.schemers.org/Documents/Standards/" target="_top">standard</a> specification
  45. (the traditional <a class="ulink" href="http://www.schemers.org/Documents/Standards/R5RS/" target="_top">R5RS</a>,
  46. <a class="ulink" href="http://www.r6rs.org/" target="_top">R6RS</a> which was ratified in 2007,
  47. and <a class="ulink" href="http://www.r7rs.org/" target="_top">R7RS</a> which was ratified in 2013),
  48. and is used by universities for both teaching and research.
  49. Scheme also has a reputation for being difficult to learn,
  50. with a weird parenthesis-heavy syntax,
  51. and hard-to-understand concepts like <a class="ulink" href="http://en.wikipedia.org/wiki/Continuation" target="_top">continuations</a>.
  52. Luckily, you don’t need to understand continuations!
  53. (Kawa doesn’t fully implement them anyway.)
  54. </p>
  55. <p>The following assumes that Kawa is already installed on your computer;
  56. if not see these <a class="link" href="Installation.xhtml" title="Getting and installing Kawa">installation instructions</a>.
  57. Running the <code class="literal">kawa</code> command in interactive mode
  58. is a good way start learning Kawa:
  59. </p>
  60. <pre class="screen"><span class="prompt">$ </span><strong class="userinput"><code>kawa</code></strong>
  61. <span class="prompt">#|kawa:1|# </span><strong class="userinput"><code/></strong>
  62. </pre>
  63. <p>If you don’t have <code class="literal">kawa</code> but you have a
  64. Kawa “jar” and you have Java installed you can instead do:
  65. </p>
  66. <pre class="screen"><span class="prompt">$ </span><strong class="userinput"><code>java -jar kawa-<em class="replaceable"><code>version-number</code></em>.jar</code></strong>
  67. <span class="prompt">#|kawa:1|# </span><strong class="userinput"><code/></strong>
  68. </pre>
  69. <p>The prompt string has the form of a Scheme comment,
  70. to make it easier to cut-and-paste.
  71. Kawa is expecting you type in an expression or command,
  72. which it will evaluate, and then print out the result.
  73. For example, a quoted string is a simple expression that evaluates to a
  74. string value, which will print as itself, before printing the next prompt:
  75. </p>
  76. <pre class="screen"><span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>"Hello, world!"</code></strong>
  77. Hello, world!
  78. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code/></strong>
  79. </pre>
  80. <p>The most noticable difference from most other programming languages
  81. is that Scheme uses “prefix” notation for function calls.
  82. For example Kawa has a function <code class="literal">max</code> which returns the
  83. largest value of the arguments.
  84. Instead of <code class="literal">max(5, 7, 3)</code>
  85. you write <code class="literal">(max 5 7 3)</code>:
  86. </p>
  87. <pre class="screen">(max 5 7 3) ⇒ 7
  88. </pre>
  89. <p>(We use the <code class="literal">⇒</code> symbol above to indicate that
  90. the expression <code class="literal">(max 5 7 3)</code> evaluates to the
  91. value <code class="literal">7</code>.)
  92. </p>
  93. <p>The prefix notation may feel a bit weird, but you quickly
  94. get used to it, and it has some advantages.
  95. One is consistency: What are special infix operators in most languages
  96. are just regular functions in Scheme.
  97. For example, addition is just a regular function call,
  98. and <code class="literal">+</code> is just a regular function name:
  99. </p>
  100. <pre class="screen">(+ 2.5 1.2) ⇒ 3.7
  101. </pre>
  102. <p>The same prefix notation is used for special operations like assignments:
  103. </p>
  104. <pre class="screen"><span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(set! sqrt-of-2 (sqrt 2))</code></strong>
  105. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code>sqrt-of-2</code></strong>
  106. 1.4142135623730951
  107. </pre>
  108. </section>
  109. <footer>
  110. <div class="navfooter">
  111. <p>
  112. Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
  113. <p>
  114. Next: <a accesskey="n" href="tutorial-Booleans.xhtml">Booleans</a></p>
  115. </div>
  116. </footer>
  117. </body>
  118. </html>