Kawa Scheme Tutorial


This is obviously incomplete, but it may be useful, especially if you're starting with Kawa from scratch. If you're new to Scheme you might also check out one of these tutorials: Takafumi Shido's Yet Another Scheme Tutorial; Greg Badross lecture notes; Dorai Sitaram's Teach Yourself Scheme in Fixnum Days; or Paul Wilson's An Introduction to Scheme and its Implementation.

Introduction

You've heard about all the hot scripting languages -- you might even be tired of hearing about them. But Kawa offers you something different than the scripting-language-du-jour can. You may be interested in one that runs on the Java virtual machine, either because you have to interact with other Java tools, or because you like having access to all the Java packages out there. Or maybe you don't care about Java, but you care about performance. If so, let me tell you about Kawa, which is actually one of the very oldest language implementations running on the Java Virtual Machine, dating back to 1996.

The Kawa language is a dialect/implementation of the Scheme language. (The Kawa project also supports other languages, including XQuery and Emacs Lisp, as well as tools for implementing mew programming languages, but we won't cover that in this tutorial.)

Scheme is an established language with many implementations, a standard specification (the established R5RS, and R6RS which was ratified in 2007), and is used by universities for both teaching and research. Scheme also has a reputation for being difficult to learn, with a weird parenthesis-heavy syntax, and hard-to-understand concepts like continuations. Luckily, you don't need to understand continuations! (Kawa doesn't fully implement them anyway.)

The following assumes that Kawa is already installed on your computer; if not see these installation instructions.

Running the kawa command in interactive mode is a good way start learning Kawa:

$ kawa
#|kawa:1|# 

If you don't have kawa but you have a Kawa jar and you have Java installed you can instead do:

$ java -jar kawa-version-number.jar
#|kawa:1|# 

The prompt string has the form of a Scheme comment, to make it easier to cut-and-paste. Kawa is expecting you type type in an expression or command, which it will evaluate and print out the result. For example a quoted string is a simple expression that evaluates to a string value, which will print as itself, before printing the next prompt:

#|kawa:1|# "Hello, world!"
Hello, world!
#|kawa:2|# 

The most noticable difference from most other programming languages is that Scheme uses prefix notation for function calls. For example Kawa has a function max which returns the largest value of the arguments. Instead of max(5, 7, 3) you write (max 5 7 3):

(max 5 7 3) ⇒ 7

(We use the symbol above to indicate that the expression (max 5 7 3) evaluates to the value 7.)

The prefix notation may feel a bit weird, but you quickly get used to it, and it has some advantages. One is consistency: What are special infix operators in most languages in most languages are just regular functions in Scheme. For example addition is just a regular ordinary function call, and + is just a regular function name:

(+ 2.5 1.2) ⇒ 3.7

The same prefix notation is used for special operations like assignments:

#|kawa:1|# (set! sqrt-of-2 (sqrt 2))
#|kawa:2|# sqrt-of-2
1.4142135623730951