Io - gregorymorrison/euler1 GitHub Wiki

Io is a very lightweight scripting language which debuted in 2002. I'm intrigued by the design of this language, although I have had minimal exposure to it. My development time for this version of Euler1 took maybe two hours - it took a while because its runtime error messages took a while to get used to...

#!/usr/bin/io
// Euler1 in IO

euler1 := method(n,
    sum := 0;

    for(i, 1, n-1,
        if ((i%3==0 or i%5==0),
            (sum := sum + i);
        );
    );
    return (sum);
);

euler1(1000) println;

IO's main language paradigm is Prototype, in which classes are implemented simply as collections of stuff. Everything in IO is an object, and all actions are messages to objects. Here's an example. I create a generic solver object inherited from Object which has one property, solution, and one method, solve. I then instantiate a child from this with which I call the solve method. Yes, it's contrived - inheritance doesn't really serve a purpose here; this is simply for illustration. This version of Euler1 took me maybe one hour to write:

// Euler1 in IO

Euler1 := Object clone
Euler1 solution := 0
Euler1 solve := method(n,
    for(i, 1, n-1,
        if ((i%3==0 or i%5==0),
            (solution = solution + i))))

euler1 := Euler1 clone
euler1 solve(1000)

euler1 solution println

And here's a tail-recursive version, which took me maybe 45 minutes to write. IO is a small language, so it doesn't take long to learn:

// Euler1 in IO

euler := method(n, acc,
    if (n == 1, return acc)

    if (n%3==0 or n%5==0,
        euler(n-1, acc+n),
        euler(n-1, acc))
)
euler1 := method(n,
    euler(n, 0))

euler1(999) println

To install on Fedora, yum install IO-languageyum has a few other supporting packages you also might be interested in. Then to execute your script, simply call it:

$ ./euler1.io
233168