Skip to content

Eryx examples

This page contains many example programs made using eryx and their corresponding output.

Simple examples

Variables

variables.eryx
let a = 1;
let b = -2;
let c = 3.14;
let d = a;
let e = "Hello, World!";
let f = true;
let g = null;
let h = [1, 2, 3];
let i = {key: "value", num: 10};

print(a);
print(b);
print(c);
print(d);
print(e);
print(f);
print(g);
print(h);
print(i);
Output
1
2
3
4
5
6
7
8
9
1
-2
3.14
1
Hello, World!
true
null
[ 1, 2, 3 ]
{ key: "value", num: 10 }

Importing

### math.eryx
func add(x, y) {
    return x + y;
}

const one = 1;
const pi = 22 / 7;


### test.eryx
import "math.eryx";
from "math.eryx" import ["add", "pi"];

print(add(5, 10), pi);
print(math.one);
Output
15 3.142857142857143
1

Functions

functions.eryx
1
2
3
4
5
func add(x, y) {
    return x + y;
}

print(add(1, 2));
Output
3

If/Else statements

if_else.eryx
1
2
3
4
5
6
7
8
let x = 10;
let y = 5;

if (x == y) {
    print("x is equal y");
} else {
    print("x is not equal y");
}
Output
x is not equal y

Loops

loops.eryx
let value = 0;
const threshold = 5;

loop {
    if (value >= threshold) {
        break;
    } else {
        value = value + 1
    }

    print(value);
}

print("done!");

value = 0;

while (value < threshold) {
    value = value + 1

    if (value % 2 != 0) {
        continue;
    }

    print(value);
}
Output
1
2
3
4
5
6
7
8
1
2
3
4
5
done!
2
4

Arithmetic operations

arithmetic.eryx
1
2
3
4
5
6
7
8
9
let x = 10;
let y = 5;

print(x + y);
print(x - y);
print(x * y);
print(x / y);
print(x % y);
print(x + y * 2 + x * (y + 2) - 5);
Output
1
2
3
4
5
6
15
5
50
2
0
85

Comparison operations

comparisons.eryx
1
2
3
4
5
6
7
8
9
let x = 10;
let y = 5;

print(x == y);
print(x != y);
print(x < y);
print(x > y);
print(x <= y);
print(x >= y);
Output
1
2
3
4
5
6
false
true
false
true
false
true

Complex examples

Factorial

Calculate the factorial of a number.

factorial.eryx
1
2
3
4
5
6
7
8
func factorial(n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

print(factorial(5));
Output
120

Fibonacci

Calculate the nth Fibonacci number.

fibonacci.eryx
func fibonacci(n) {
    if (n <= 0) {
        return 0;
    }
    if (n == 1) {
        return 1;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

print(fibonacci(10));
Output
55

GCD

Find the greatest common divisor of two numbers using Euclid's algorithm.

gcd.eryx
1
2
3
4
5
6
7
8
func gcd(a, b) {
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

print(gcd(48, 18));
Output
6

Prime Check

Check if a number is a prime number.

is_prime.eryx
func isPrime(n, divisor) {
    if (n <= 1) {
        return false;
    }
    if (divisor == 1) {
        return true;
    }
    if (n % divisor == 0) {
        return false;
    }
    return isPrime(n, divisor - 1);
}

let number = 17;
print(isPrime(number, int(number ** 0.5)));
Output
true

Offset adder

Create an adder with an offset then use it.

adder.eryx
func makeAdder(offset) {
    func add(x, y) {
        return x + y + offset;
    }

    return add;
}

let adder = makeAdder(10);
print(adder(5, 10));
Output
25

Sum digits

Sum all digits of a number.

sum_digits.eryx
1
2
3
4
5
6
7
8
func sumOfDigits(n) {
    if (n == 0) {
        return 0;
    }
    return (n % 10) + sumOfDigits(int(n / 10));
}

print(sumOfDigits(12345));
Output
15

Reverse number

Reverse the digits of a number.

reverse_number.eryx
1
2
3
4
5
6
7
8
9
func reverseNumber(n, reversed) {
    if (n == 0) {
        return reversed;
    }
    reversed = reversed * 10 + (n % 10);
    return reverseNumber(int(n / 10), reversed);
}

print(reverseNumber(12345, 0));
Output
54321

Is sorted

Check if a list is sorted.

is_sorted.eryx
func isSorted(arr, idx) {
    if (idx == 0) {
        return true;
    }
    if (arr[idx] < arr[idx - 1]) {
        return false;
    }
    return isSorted(arr, idx - 1);
}

let nums = [1, 2, 3, 4, 5];
print(isSorted(nums, len(nums) - 1));
Output
true