The Sway Reference Manual/Primitives

Sway works by figuring out the meaning or value of some code. This is true for the tiniest pieces of code to the largest programs. The process of finding out the meaning of code is known as evaluation.

The things whose values are the things themselves are known as primitives. The primitives of Sway can be categorized by the following types: integers, real numbers, strings, and symbols.

Sway (or more correctly, the Sway interpreter) responds to primitives by giving the type of the value plus the value itself.

Real numbers must start with a digit or a minus sign or a decimal point. An initial minus sign must immediately be followed by a digit or a decimal point. An initial decimal point must immediately be followed by a digit. Sway accepts real numbers in scientific notation. For example, would be entered as 3.0e-11. The 'e' stands for exponent and the 10 is understood, so e-11 means multiply whatever precedes the e by .

The Sway interpreter only displays between six and eleven significant digits, depending on the size of the number, so 1.234567898765432 would display as:

sway> 1.234567898765432;
REAL: 1.2345678988

Don't be fooled. Although the result looks like 1.2345678988, it is not. The following code tests if the two numbers are equal:

sway> 1.234567898765432 == 1.2345678988;
SYMBOL: :false

Numbers greater than and less than are displayed in scientific notation.

To display more (or less) significant digits, the fmt function can be found. Here's how to display 15 digits after the decimal point:

sway> fmt("%.15f",1.234567898765432);
STRING: 1.234567898765432

The fmt function uses the same format codes as the C printf function.

Don't try and display more than 16 significant digits; the least significant digits will not be accurate.

Characters in a string can be escaped with the backslash character, which changes the meaning of some characters. For example, the character 'n', in a string refers to the letter 'n' while the character sequence '\n' refers to the newline character. A backslash also changes the meaning of the letter 't', converting it into a tab character '\t'. When other characters are escaped, their meanings are not changed. Thus '\z' is equivalent to 'z'. To include a backslash character in a string, escaped it with a backslash, '\\'. Note that Sway, when asked the value of strings that contain newline and tab characters, displays them as escaped characters. When newline and tab characters in a string are printed in a program, however, they are displayed as actual newline and tab characters, respectively. Double quotes can be embedded in a string by escaping them with backslashes. A string with no characters between the double quotes is known as an empty string.

Unlike some languages, there is no character type in Sway. A single character 'a', for example, is entered as the string "a".

Sway symbols are collections of certain characters beginning with a colon:

sway> :abc;
SYMBOL: :abc
sway> :a+b;
SYMBOL: :a+b

When symbols are printed in a program, the colon is omitted from the output. Symbols may contain any of the printable characters except parentheses, braces, semicolons, and commas. The colon marks the beginning of the symbol while whitespace (newlines, tabs, and spaces) and the prohibited characters signal the end of the symbol. As we shall see later, symbols are useful for setting up symbolic constants and for passing messages.

There are a few special symbols in Sway, two of which are :true and :false. These symbols are known as the Boolean values and are used to guide the flow of a program. The term Boolean is derived from the last name of George Boole, who, in his 1854 paper "An Investigation of the Laws of Thought, on which are founded the Mathematical Theories of Logic and Probabilities", laid one of the cornerstones of the modern digital computer. The so-called Boolean logic or Boolean algebra is concerned with the rules of combining truth values (i.e., true or false). As we will see, knowledge of such rules will be important for making Sway programs behave properly. In particular, Boolean expressions will be used to control conditionals and loops.

Another special symbol is :null. This symbol is used to indicate the end of lists and arrays; it also indicates an 'not yet created' object.,