Skip to main content

Examples

Each example below is a complete program. Save it to a .chuchu file, then compile and run it:

cheeku example.chuchu -o example
./example

Circle area​

assign pi to 3.14159
assign radius to 5.0
assign area to pi * radius * radius
show area

Output: 78.53975.

Factorial​

function factorial(n: int) -> int {
if n <= 1 {
return 1
} else {
return n * factorial(n - 1)
}
}
show factorial(5)

Output: 120.

Dot product​

assign a to [1, 2, 3]
assign b to [4, 5, 6]
show sum(a * b)

Output: 32. Multiplication creates an element-wise product; reduction sums it.

A typed function with widening​

function half(x: float) -> float {
return x / 2.0
}
show half(9)

Output: 4.5. The integer argument widens to float.

Nested iteration​

assign i to 0
assign total to 0
while i < 3 {
assign j to 0
while j < 2 {
reassign total to total + i * j
reassign j to j + 1
}
reassign i to i + 1
}
show total

Output: 3.

More examples in the repository​

The compiler examples directory contains arithmetic, conditions, loops, functions, floats, arrays, and a syntax smoke test. The benchmark sources exercise longer numerical workloads.

For a complete list of supported constructs, see language status.