Io

Implementation of programming language Io

Author’s implementation of Io. Written in C.

Examples:

Hello, World!:

Example for versions Io-2008-01-07

This program creates a symbol “Hello, World!” (a symbol is an immutable sequence) and sends it a println message (similar to calling println method in other languages).

"Hello, World!" println

Factorial:

Example for versions Io-2008-01-07

This example uses iterative factorial definition. Program output for large numbers looks in the following way:

12! = 479001600
13! = 6.227021e+009
14! = 8.717829e+010
15! = 1.307674e+012
16! = 2.092279e+013
F := 1;
for(N,0,16,
   N print;
   "! = " print;
   F println;
   F := F * (N+1);
);

Fibonacci numbers:

Example for versions Io-2008-01-07

This example uses iterative definition of Fibonacci numbers. for loop misses fourth parameter which sets loop step — it defaults to 1, so no need to set it explicitly.

N0 := 0;
N1 := 1;
for(i,1,16,
   N2 := N1+N0;
   N0 := N1;
   N1 := N2;
   N0 print;
   ", " print;
);
"..." println; 

Fibonacci numbers:

Example for versions Io-2008-01-07

This example uses Binet’s formula. Math functions are called by sending a message to the number which is an object as well.

g := (5 sqrt + 1) / 2;
for(i,1,16,
   N := ((g pow(i)) - ((1-g) pow(i))) / (5 sqrt);
   N round print;
   ", " print;
);
"..." println; 

Quadratic equation:

Example for versions Io-2008-01-07

To output several pieces of data in one line, a simple trick is used: these pieces of data form a list, and then the list is concatenated using join command, and the result is printed as one string.

A := File standardInput readLine asNumber;
if(A==0, 
   "Not a quadratic equation." println;
   return;
);
B := File standardInput readLine asNumber;
C := File standardInput readLine asNumber;
D := B*B-4*A*C;
A2 := 2*A;
if(D==0,
   list("x = ", (-B/A2) asString) println;
   return;
);
sqrtD := D abs sqrt;
if(D>0,
   list("x1 = ", ((-B+sqrtD)/A2) asString) join println;
   list("x2 = ", ((-B-sqrtD)/A2) asString) join println,
   list("x1 = (", (-B/A2) asString, ", ", (sqrtD/A2) asString, ")") join println;
   list("x2 = (", (-B/A2) asString, ", ", (-sqrtD/A2) asString, ")") join println;
);

CamelCase:

Example for versions Io-2008-01-07

This example shows char-by-char string processing. Note that at(i) returns ASCII-code of i-th character of the string, and slice(i,i+1) returns the character itself as a string of length 1. The check whether the character is a letter is done using ASCII-codes, and conversion to uppercase and string output — using string representation.

S := File standardInput readLine asLowercase;
lastSpace := 1;
for(i,0,(S size)-1,
   ascii := S at(i);
   letter := S slice(i,i+1);
   if(ascii>=97 and ascii<=122,
      if(lastSpace==1, letter := letter asUppercase);
      letter print;
      lastSpace := 0,
      lastSpace := 1;
   );
);