Skip to content

Hello, world!

Let’s write your first prepoly program!

Write the following program into a hello.pp file.

println("Hello, world!")

Then, execute the program:

Terminal window
prepoly hello.pp

The output is as follows:

Hello, world!

A prepoly source file is a script: top-level statements run from top to bottom. You can also define a main function, which is called after the top-level statements have run:

fun main() {
println("Hello, world!")
}

The execution result is the same as the previous one.

Every function is fully type-checked before it runs. To type-check a program without executing it, use prepoly check:

Terminal window
prepoly check hello.pp

It prints ok when the program is well-typed, or the type errors otherwise. Running prepoly with no arguments starts an interactive REPL.

# starts a line comment, so a source file may begin with a shebang line. That lets you run a prepoly file directly, like a shell script:

#!/usr/bin/env prepoly
println("Hello from a script!")

Mark the file executable once, then run it by name:

Terminal window
chmod +x hello.pp
./hello.pp

The shebang line is ordinary comment syntax, so the same file still works with prepoly hello.pp, prepoly check hello.pp, and on systems that do not use shebangs at all.

Next, let’s write a practical example.

We can write a gcd function, which calculates the greatest common divisor, as follows:

fun gcd(a, b) {
if b == 0 {
return a
} else {
return gcd(b, a % b)
}
}
println(gcd(48, 36))

This outputs 12, which is correct!

Note that we didn’t write a single type annotation: parameter and return types are inferred. The program is still statically typed — passing a string to gcd would be rejected before execution.

We can use const to declare an immutable variable and let to declare a mutable variable.

const pi = 3.14159 // reassigning is a compile error
let count = 0
count += 1

The following program calculates the gcd of all elements in an array:

fun gcd(a, b) {
if b == 0 {
return a
} else {
return gcd(b, a % b)
}
}
const elems = [16, 36, 72, 192]
let result = elems[0]
for elem in elems.slice(1, elems.len()) {
result = gcd(result, elem)
}
println("GCD is {result}")

This program outputs GCD is 4.

The {result} inside the string is string interpolation: {expr} evaluates the expression and inserts its text into the string.

Now you have seen a complete little program. The following chapters introduce each language feature the same way — by example. For exhaustive rules, see the references.