Hello, Kitten!

The first program to write in any new language is the classic hello, world, which simply outputs a greeting and exits. Fire up the Kitten interpreter by entering kitten in your terminal:

$ kitten
Welcome to Kitten! Type //help for help or //quit to quit.

Try the //help and //quit commands suggested by the interpreter. In normal Kitten code, two slashes indicate a comment which continues to the end of the line, which you can use to document parts of your code. Because ordinarily you won’t be writing comments in the interactive interpreter, instead it treats single-line comments as commands for the interpreter rather than code.

Now try entering "meow" say into the interpreter:

>>> "meow" say
meow

This program consists of a single expression, "meow" say, with two subexpressions: "meow" is a text literal, representing a list of characters. say is the name of a function, called a word. When you enter this expression, the interpreter checks it for errors, executes it, and shows the result.

Every expression in Kitten denotes a function, which takes some inputs and produces some outputs, possibly with side effects such as printing text to the console. Likewise, every expression has a type describing these inputs and outputs. You can see the type of an expression using the //type command in the interpreter, followed by an expression:

>>> //type "meow"
-> List<Char>

>>> //type say
<T> (T -> +IO) where (show<T>)

The type of a function is denoted by a rightwards arrow, with the types of the inputs on the left, and the types of the outputs on the right. "meow" is a function that takes no inputs (there’s nothing to the left of the arrow) and produces one output of type List<Char>—that is, a list of characters.

The type of say is a little more complex, and we’ll revisit it in detail later on. It describes the fact that say takes a value of any type T that is printable, produces no result values, and does some I/O to display the value on the console.

Stack-based Programming

You can write the above program across two lines:

>>> "meow"
"meow"

>>> say
meow

Kitten is a stack-based language, meaning that all functions take their inputs by popping them from an implicit data stack, and return their results by pushing them onto that same stack.

So the text literal "meow" pushes a string value to the stack, which you can see displayed as an intermediate result in the interpreter. Then the say word pops and consumes that value, writing it to standard output followed by a newline.

You can place any kind of data on the stack—it’s heterogeneous. For example, try entering a math expression using the operators + (add), - (subtract), * (multiply), and / (divide):

>>> (2 + 3) * 5
25

These expressions are written with infix operators for convenience and familiarity with other languages. But they’re syntactic sugar for stack-based operations—if you wrap an operator in parentheses, it behaves like a normal postfix Kitten function:

>>> 2 3 (+)  // push 2 and 3 and add them
5

>>> 5 (*)    // push 5 and multiply it by the result
25

And because program fragments are joined with simple function composition, we can split this up in different ways to see the intermediate results on the stack:

>>> 2
2

>>> 3
3
2

>>> (+)
5

>>> 5
5
5

>>> (*)
25

Note that the stack is displayed in top-to-bottom order: the topmost element on the stack is displayed first, and the bottommost element last.

results matching ""

    No results matching ""