The basics of Kaya programs

A simple Kaya program

As is traditional, the Hello World! program in Kaya.

program hello;

Void main() {
    // My first program!
    putStrLn("Hello world!");
}

Compile the program by saving the above code as 'hello.k' and using kayac hello.k and run it with ./hello - the result should be something like this.

$ kayac hello.k
Compiling program hello
$ ./hello
Hello world!
$ 

Even though this is a simple program, it introduces several features of Kaya

A simple Kaya webapp

webapp webhello;

import Webapp;
import HTMLDocument;

HTMLDocument webmain() {
    doc = HTMLDocument::new(HTML4Strict,"Hello Web!");
    h1 = addHeading(doc.body,1,"Hello Web!");
    
    return doc;
}

To compile this, save the code above as webhello.k and type kayac webhello.k. This will give you a file called webhello.cgi - you can run this like any other program, but it's more interesting put on a web server that can run CGI applications (you may need to put them in a special directory, often called cgi-bin, or it may be sufficient for the program to have the cgi filename ending.

If you don't currently have a suitable web server available, then most of the web examples are run on kayalang.org too - here's Hello Web!

Of course, for something so simple, you could just use a static HTML file, but it's useful to test that your web server is set up correctly for CGI. The HTML source code produced by the application should look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
	     "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>Hello World!</title>
 </head>
 <body>
  <h1>Hello World!</h1>
 </body>
</html>

This webapp introduces some extra syntax:

Other features of the syntax will be made clearer in the rest of this tutorial and the data types tutorial.

After 'Hello World'

The rest of this tutorial has been split into four parts to keep individual pages short. It is probably best to read them in the order below. After you have read them, you should be able to do the exercises below.

  1. Variables, arrays and data types
  2. Program flow and control
  3. Functions
  4. Input and output

Exercises

  1. Write a program that finds and prints the prime numbers between 2 and 1000. The elem function may be useful.
  2. Write a program that prints the characters in the String "Hello world!" in hexadecimal, separated by spaces. The getIndex and stringBase functions will be useful.
  3. Write a function that can be used with filter to find all the square numbers in an list of integers.
  4. Write a function that uses recursion to calculate the Ackermann function, defined as:
    • Ackermann(0,y) = y+1
    • Ackermann(x,0) = Ackermann(x-1,1)
    • Ackermann(x,y) = Ackermann(x-1,Ackermann(x,y-1))
    To check that it works, Ackermann(3,6) is 509. You will probably find that calculating the function with parameters larger than that takes a significant amount of time - use the -nortchecks option when compiling to speed things up a bit.
  5. Rewrite the final copier program from the input and output tutorial so that rather than simply copying the file, it reverses each line as it copies it (but leaves the new line alone at the end).
  6. Write a program that reads numbers from standard input, one per line, and if given a blank line, prints the sum of all numbers so far. It should exit if the end of the file is reached, or if the input is not a number. The isDigit function will check whether a single character is a digit or not, but you will have to work out how to apply this to an entire String.
kaya@kayalang.org | Last modified 4 September 2008 | Supported by Durham CompSoc | Powered by Kaya