Examples

A collection of Period programs demonstrating the language's features.

Hello World

-- The traditional first program.
let greeting be "Hello, World!".
show greeting.

Factorial

define factorial with n:
    if n <= 1 then:
        return 1.
    return n * factorial with (n - 1).

show factorial with 5.

Fibonacci Sequence

define fib with n:
    if n <= 1 then:
        return n.
    return fib with (n - 1) + fib with (n - 2).

let i be 0.
while i < 10 repeat:
    show fib with i.
    set i to i + 1.

Prime Numbers

-- Find the first 20 prime numbers.
let count be 0.
let candidate be 2.

while count < 20 repeat:
    let is_prime be true.
    let divisor be 2.
    while divisor * divisor <= candidate repeat:
        if candidate % divisor == 0 then:
            set is_prime to false.
        set divisor to divisor + 1.
    if is_prime then:
        show candidate.
        set count to count + 1.
    set candidate to candidate + 1.

Type Annotations

-- Type annotations are optional.
define add with number a, number b returns number:
    return a + b.

define greet with string name returns string:
    return "Hello, " + name + "!".

show add with 3, 4.
show greet with "Period".

Read and Greet

show "What is your name?".
let name be input.
show "Nice to meet you, " + name + ".".

Lists and Dictionaries

let items be [1, 2, 3].
set items[0] to 10.
show items.

let person be {"name": "Ada", "age": 36}.
show person["name"].
set person["age"] to 37.
show person.

Imports

-- Import multiple modules at once.
import math, random and string.

let greeting be upper with "hello".
show sqrt with 2.

-- If two modules export the same name, qualify it with from.
show sin from math with 5.

Classes

-- Define a class with init and methods.
class Person:
    init with name, age:
        set this name to name.
        set this age to age.

    define greet with greeting:
        show greeting + ", " + this name + "!".

let ada be new Person with "Ada", 42.
tell ada to greet with "Hello".
show the age of ada.