Factorial in S-lang

Example for versions S-lang 2.2.2

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}"$);
};