Seed7

Implementation of programming language Seed7

An implementation of Seed7 developed by the language author. It is written in C and distributed under GNU GPL (interpreter and examples) and GNU LGPL (runtime library) licenses.

Examples:

Hello, World!:

Example for versions Seed7 2012-01-01
$ include "seed7_05.s7i";

const proc: main is func
begin
    writeln("Hello, World!");
end func;

Factorial:

Example for versions Seed7 2012-01-01

This example uses built-in factorial function !n. It is defined only for integer data type, so trying to calculate 13! causes an overflow. The program output looks as follows:

0! = 1  
1! = 1  
2! = 2  
...
11! = 39916800  
12! = 479001600  
13! =  
*** Uncaught EXCEPTION NUMERIC_ERROR raised with
{! integer <80ba990>: <SYMBOLOBJECT> 0 }

{! (in integer <80ba990> param) } at factorial-builtin.sd7(10)
main no POSINFO
$ include "seed7_05.s7i";

const proc: main is func
local
    var integer: n is 0;
begin
    for n range 0 to 16 do
        writeln(n <& "! = " <& !n);
    end for;
end func;

Factorial:

Example for versions Seed7 2012-01-01

This example uses recursive factorial definition. Factorial values are stored as bigInteger, so there is no overflow.

$ include "seed7_05.s7i";
  include "bigint.s7i";

const func bigInteger: factorial (in var bigInteger: n) is func
result
    var bigInteger: result is 1_;
begin
    if n = 0_ then
        result := 1_;
    else
        result := n * factorial(n - 1_);
    end if;
end func;

const proc: main is func
local
    var integer: n is 0;
begin
    for n range 0 to 16 do
        write(n); 
        write("! = ");
        write(factorial(bigInteger conv n)); 
        writeln;
    end for;
end func;

Fibonacci numbers:

Example for versions Seed7 2012-01-01

This example uses recursive definition of Fibonacci numbers.

$ include "seed7_05.s7i";

const func integer: fibonacci (in var integer: n) is func
result
    var integer: result is 1;
begin
    if n < 2 then
        result := 1;
    else
        result := fibonacci(n - 1) + fibonacci(n - 2);
    end if;
end func;

const proc: main is func
local
    var integer: n is 0;
begin
    for n range 0 to 15 do
        write(fibonacci(n) <& ", ");
    end for;
    writeln("...");
end func;

CamelCase:

Example for versions Seed7 2012-01-01

This example uses character-by-character processing.

$ include "seed7_05.s7i";

const proc: main is func
local
    var string: text is "";
    var string: camel_case is "";
    var char: ch is ' ';
    var boolean: was_space is TRUE;
begin
    readln(text);
    text := lower(text);
    for ch range text do
        if ch in {'a' .. 'z'} then
            if was_space then
                ch := upper(ch);
            end if;
            camel_case &:= ch;
            was_space := FALSE;
        else
            was_space := TRUE;
        end if;
    end for;
    writeln(camel_case);
end func;

Quadratic equation:

Example for versions Seed7 2012-01-01
$ include "seed7_05.s7i";
  include "float.s7i";
  include "math.s7i";

const proc: main is func
local
    var float: a is 0.0;
    var float: b is 0.0;
    var float: c is 0.0;
    var float: d is 0.0;
begin
    readln(a);
    if a = 0.0 then
        writeln("Not a quadratic equation.");
    else
        readln(b);
        readln(c);
        d := b ** 2 - 4.0 * a * c;
        if d = 0.0 then
            writeln("x = " <& (-b / 2.0 / a));
        else
            if d > 0.0 then
                writeln("x1 = " <& ((-b + sqrt(d)) / 2.0 / a));
                writeln("x2 = " <& ((-b - sqrt(d)) / 2.0 / a));
            else
                writeln("x1 = (" <& (-b / 2.0 / a) <& "," <& (sqrt(-d) / 2.0 / a) <& ")");
                writeln("x2 = (" <& (-b / 2.0 / a) <& "," <& (-sqrt(-d) / 2.0 / a) <& ")");
            end if;
        end if;
    end if;
end func;