This chapter describes I/O of Egison. Since Egison is purely functional, Egison I/O has its own special syntax.

Hello World!

First of all, let's greet the world using Egison. The following is a "Hello world" program in Egison.

We can execute the above program as follow.

% egison hello.egi
Hello, World!

Egison I/O works via a function named main. In main, we can use I/O functions such as write, which we cannot use in the others. (The list of I/O primitive functions is given in the next chapter Primitive Functions.)

The Command Line Arguments

The collection of them is given for main as its argument.

If you run the interpreter with a filename as its first argument, the interpreter executes the function main. Like the above case, the arguments other than the filename hand main.

For instance, assume we create args.egi.

Then, executing the following commands, you can find the arguments are given for main as $args.

$ egison args.egi
$ egison args.egi We can write scripts in Egison
{"We" "can" "write" "scripts" "in" "Egison"}

do Expressions

(do {statement ...} expr)
  statement ::= expr
              | [variable expr]

To do more complicated I/O tasks, we can use do expressions, which is similar to the let and letrec expressions. The feature of the do expressions is serial execution. If you know Haskell, you probably notice that it is the same with the do expressions in Haskell. Recall many I/O functions have no return values. In such cases, we can omit to write a variable.

Then, execute it as follow.

$ egison repl.egi
input: Hi
input: Hello
input: Repl
input: ^C

When the code is executed, (write "args: "), (read-line) and (write input) are executed in the order.

There are several samples in the directory sample/io/. Please check them.

io Expressions

We can use an io expression to call an IO function as an ordinary function. The following is a definition of the pure-rand function in lib/core/io.egi.

We can use an io expression on the interpreter, too.

$ egison
Egison Version 3.3.15 (C) 2011-2014 Satoshi Egi
Welcome to Egison Interpreter!
> (io (each (compose show print) (match-all primes (list integer) [<join _ <cons $p <cons ,(+ p 2) <cons ,(+ p 6) <cons ,(+ p 8) _>>>>> [p (+ p 2) (+ p 6) (+ p 8)]])))
[5 7 11 13]
[11 13 17 19]
[101 103 107 109]
[191 193 197 199]
[821 823 827 829]
[1481 1483 1487 1489]
[1871 1873 1877 1879]
[2081 2083 2087 2089]
[3251 3253 3257 3259]
[3461 3463 3467 3469]
[5651 5653 5657 5659]

What to do next...

Next Chapter: Primitive Functions Top of Manual Back to Home