S-lang 2.2.2

Version of implementation S-lang of programming language S-lang

Version of S-lang.


Hello, World! - S-lang (158):

This example should be executed in slsh interpreter.

message ("Hello, World!");

Factorial - S-lang (159):

This example uses recursive factorial definition and features the usage of recursive functions in S-lang. To create a recursive function, you have first to declare it without listing parameters or implementation — define factorial ();, and then actually define it, with the list of parameters and function body.

Another feature included in this example is usage of $ suffix. A string literal can end with a suffix which defines the way of processing this string. $ suffix requires that variable name substitution is performed on the string before using it. This means that each variable name prefixed by $ character within the string will be replaced with variable value. Variable name can be enclosed in {} braces to separate it from the string contents; the braces can be omitted if variable name is followed by a space.

Note that the default numeric data type in S-lang is integer, so the given example produces overflow error when calculating 13!.

define factorial ();

define factorial (n)
{   if (n==0) return 1;
     return n * factorial (n-1);

for (i=0; i<17; i++)
{   f = factorial (i);
    message ("${i}! = ${f}"$);

Factorial - S-lang (160):

This example shows array operations in S-lang. [1:i] creates a list of numbers from 1 to i; note that the default data type for array operations is double even if the numbers are integer. Intrinsic function prod (since version 2.1) returns the product of elements of its parameter. Intrinsic function sprintf provides C-style printing, %.0f printing floating point number with zero digits after decimal comma.

for (i=0; i<17; i++)
    sprintf ("%d! = %.0f", i, prod ( [1:i] ) );

Fibonacci numbers - S-lang (161):

This example uses iterative definition of Fibonacci numbers and shows some more features of array processing in S-lang. f is explicitly declared as an array of 16 integer numbers. Elements 0 and 1 of f are set to 1; here [0:1] creates a list of indices to which the operation is applied. Intrinsic function string converts its argument to its string representation.

f = Integer_Type [16];
f[[0:1]] = 1;
for (i=2; i<16; i++)
    f[i] = f[i-1] + f[i-2];
s = "...";
for (i=15; i>=0; i--)
    s = string(f[i]) + ", " + s;
message (s);