Examples

Here are a few small Hurl programs. More examples are in the repo.

Fizzbuzz

include "../lib/loop.hurl";
include "../lib/if.hurl";

let fizzbuzz = func(locals) {
    let x = locals.1;
    let max = locals.2;

    let printed = false;

    if(func() {
        hurl (x % 3 == 0);
    }, func() {
        print("fizz");
        printed = true;
    });

    if(func() {
        hurl (x % 5 == 0);
    }, func() {
        print("buzz");
        printed = true;
    });

    if(func() {
        hurl ~printed;
    }, func() {
        print(x);
    });

    println();

    toss [x + 1, max];
    hurl ~((x + 1) == max);
};

try {
    loop(fizzbuzz, [1, 101]);
} catch as retval {
    println("Final: " + retval);
};

Fibonacci

include "../lib/loop.hurl";

# fibonacci in hurl
let fib = func(locals) {
    let a = locals.1;
    let b = locals.2;
    let iters = locals.3;
    let max_iter = locals.4;

    toss [b, a + b, iters + 1, max_iter];
    hurl iters < max_iter;
};

try {
    loop(test, [0, 10]);
} catch as retval {
    println("done");
};

Factorial

let factorial = func(n) {
    try {
        hurl n == 0;
    } catch (true) {
        hurl 1;
    } catch (false) {
        let next = 1;
        try {
            factorial(n - 1);
        } catch into next;
        hurl n * next;
    };
};

try {
    factorial(10);
} catch as x {
    println("factorial(10) = ", x);
};

Huh, all the examples start with "F". Weird.