S (programming language)

From Bauman National Library
This page was last modified on 8 June 2016, at 14:35.
S
Paradigm multi-paradigm: imperative, object oriented
Developer Rick Becker, Allan Wilks, John Chambers
Typing discipline dynamic, strong
License depends from implementation
Website ect.bell-labs.com/sl/S/
Major implementations
R, S-PLUS
Influenced by
C, APL, PPL, Fortran

S-lang - script programming language that supports operations on arrays.

S is a language and system for organizing, visualizing, and analyzing data. It has been a project of statistics research at Bell Labs since 1976, evolving continually through that time. In 1998, S became the first statistical system to receive the Software System Award, the top software award from the ACM.

S has from the start been aimed at programming with data; that is, at describing to the computer some graphical view, numerical summary, statistical model, or other information you want to produce. It occupies a middle ground between packages that emphasize standard operations and research projects in language design that start from a more abstract goal. S has always been designed to be used in practice, but with an emphasis on users who wanted to turn new ideas into software.

Interpreter is designed to be embedded in other software but can be used as a standalone interpreter slsh.

The two modern implementations of S are:

  1. R - part of the GNU free software project
  1. S-PLUS - A commercial product sold by TIBCO Software.

History

"Old S"

S is one of several statistical computing languages that were designed at Bell Laboratories, and first took form between 1975–1976. Up to that time, much of the statistical computing was done by directly calling Fortran subroutines; however, S was designed to offer an alternate and more interactive approach. Early design decisions that hold even today include interactive graphics devices (printers and character terminals at the time), and providing easily accessible documentation for the functions.

In 1980 the first version of S was distributed outside Bell Laboratories and in 1981 source versions were made available. In 1984 two books were published by the research team at Bell Laboratories: S: An Interactive Environment for Data Analysis and Graphics (1984 Brown Book) and Extending the S System. Also, in 1984 the source code for S became licensed through AT&T Software Sales for education and commercial purposes.

"New S"

By 1988, many changes were made to S and the syntax of the language. The New S Language (1988 Blue Book) was published to introduce the new features, such as the transition from macros to functions and how functions can be passed to other functions (such as apply). Many other changes to the S language were to extend the concept of "objects", and to make the syntax more consistent (and strict). However, many users found the transition to New S difficult, since their macros needed to be rewritten. Many other changes to S took hold, such as the use of X11 and PostScript graphics devices, rewriting many internal functions from Fortran to C, and the use of double precision (only) arithmetic. The New S language is very similar to that used in modern versions of S-PLUS and R.

In 1991, Statistical Models in S (1991 White Book) was published, which introduced the use of formula-notation (which use the '~' operator), data frame objects, and modifications to the use of object methods and classes.

S4

Version 4 of S, often abbreviated S4, provides advanced object-oriented features. S4 classes differ markedly from S3 classes.

Syntax elements

Note to the end of the line
%
Assigning a value to a variable
varname = value
Variable declaration
variable varname
Declaring a variable with assignment value
varname = value
Grouped expressions
( ... )
Block
{ ... }
Compare
< > <= >=
Defining functions
define functionName (argname1, ..., argnameN)
Calling functions
functionName(arg1, ..., argN)
Calling a function without parameters
functionName()
Sequence
;
If - then
if (condition) trueBlock
If - then - else
if (condition) trueBlock else falseBlock
The cycle with the precondition
while (condition) loopBody
The cycle with postcondition
do loopBody while (condition)
Cycle for - next to the range of integers with an increment of 1
_for i (first, last, 1) loopBody
Cycle for - next to the range of integers with a decrement of 1
_for i (last, first, -1) loopBody

Defining Classes and Methods

Methods in S define how a particular function should behave, based on the class of the arguments to the function. You can find yourself involved in programming new methods for several reasons, usually arising either from working with a new class of objects or with a new function:# You need to define a new class of objects to represent data that behaves a bit differently from existing classes of objects.

  1. Your application requires a new function, to do something different from existing functions, and it makes sense for that function to behave differently for different classes of objects.

You may also just need to revise an existing method or differentiate classes that were treated together before. However it comes about, defining a new method is the workhorse of programming with objects in S.

Conversely, defining a new class is less common, but often the crucial step. Classes encapsulate how we think about the objects we deal with, what information the objects contain, what makes them valid. You will likely write many more methods than class definitions, particularly since each new class definition typically generates a number of new methods. But the usefulness of your project will depend on good design of the object classes, probably more than anything.

Around now we realize that the generic purpose of the function (in this case, to produce an informative one-line summary) needs to be implemented by different methods for different kinds of objects. The class/method mechanism in S provides exactly this facility. We will define methods for those classes of objects where we can see a useful, simple summary. The existing function still plays a role, now as the default method, to be used when none of the explicit methods applies. For this purpose we will want to return to a simple definition, say:

 whatis <- function(object) paste("An object of class", + data.class(object))

Examples

Hello, World!

Example for version S-lang 2.2.2

message ("Hello, World!");

Factorial

Example for version S-lang 2.2.2

This example uses recursive definition of factorial. To create a recursive function in S-lang, you must first declare it (without a list of parameters, or implementation), and only then actually define it by specifying the parameters and functions of the body.

The example illustrates the use of the suffix $. A string constant may end with a suffix that defines how it is processed. $ Suffix means that before using the line there will be produced the substitution variable names: each variable name preceded by the prefix $, will be replaced by the variable value. The name of the variable should be separated from the next by a space or a character string enclosed in braces.

The default data type is integer, so in this example, there is an 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}"$);
};

This example demonstrates the array operations in S-lang. [1: i] creates a list of numbers from 1 to i; It should be noted that operations on arrays default data type is double, even if they are integers. Integrated prod (since version 2.1) calculates the product of elements of the array argument. Built sprintf function provides output in the style of the language C, the pattern % .0f prints floating point number with zero decimal places.

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

Fibonacci numbers

Example for version S-lang 2.2.2

The example uses iterative definition of Fibonacci numbers. The variable f explicitly defined as an array of 16 integers. The elements of the array 0 and 1 are set to 1: this operation [0: 1] generates a list of codes, which should be applied to the operation. Built-in string function 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);

References

  1. , S-Lang Library
  2. , S-lang
  3. , A Guide to the S Language
  4. , Classes and Methods in the S Language