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
program hellois the 'header' and tells the compiler that this file is a normal program to be compiled to an executable called 'hello' ('hello.exe' on Windows). Other headers instead make files into library modules or web applications - see later tutorials for more information.- All Kaya code (as opposed to data definitions, global variables, etc) must be contained inside a function definition. This short program only has one function, defined by '
Void main() {'. This defines a functionmain, of return typeVoid(i.e. it returns nothing), with no parameters. - In programs, the
mainfunction is special - it must always be present and of typeVoid, and will be run automatically when the program starts. It will therefore call all other functions (directly or indirectly) used by the program. - putStrLn is a function defined in the Kaya standard prelude. The standard prelude defines a small set of core functions available to all Kaya programs. Documentation for all functions available in the standard prelude and other Kaya libraries is included in the library reference.
//is a comment marker - everything after it on the line is ignored. C-style/* ... */comments may also be used.
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:
- The header for a webapp is
webapp ...;rather thanprogram ...;. This tells the compiler that the compiled file should have a .cgi extension, and should use the CGI input and output routines (which handle things like reading GET and POST data, setting HTTP headers, and so on). - The main function for a webapp,
webmain, has a return type ofHTMLDocument, so at the end of the function, it must usereturnto return a value of that type. - The webapp uses functions from the HTMLDocument and Webapp modules, and so these modules must be made available to the program using the
importstatement - more on this in the modules tutorial.
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.
Exercises
- Write a program that finds and prints the prime numbers between 2 and 1000. The elem function may be useful.
- 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.
- Write a function that can be used with filter to find all the square numbers in an list of integers.
- 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))
-nortchecksoption when compiling to speed things up a bit. - 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).
- 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.