The Science of Programming/The Simplest Things/Sway Primitives

From testwiki
Jump to navigation Jump to search

Sway Primitives

Consider a computer program for translating one human language into another. The simplest task one might ask of this program is the meaning of a word. For example, one might ask the program the meaning of the word 'drei'. If the program is set up to translate from German to English, the answer would be 'three'. By analogy, the simplest program one could think of also requests the meaning of a word, only in this case, one would ask the meaning of a word in a particular programming language. To make things rather self-referential, let's imagine the program gives the meaning of words in the very language used to write the program! The Sway interpreter can answer those types of requests quite easily: To see that this is so, one starts up the Sway interpreter by issuing the command sway in response to the system prompt.

   % sway
   sway>

Here, the % represents the system prompt. The interpreter signals its readiness to handle requests by issuing the Sway prompt sway>. Consider someone typing the number 3 and then a semicolon at the Sway prompt:

   sway> 3;
   INTEGER: 3

In the remainder of the text, the person who started up the Sway interpreter (or is running a Sway program) is known as the user. The user has just asked the Sway interpreter to evaluate or give the meaning of 3. The semicolon after the 3 can be thought of as a signal to the interpreter that the request has been completed, much like the period at the end of a sentence. The interpreter reads in the request and responds with the phrase INTEGER: 3. In this text, requests to the interpreter will, in general, be preceded by the Sway prompt. Responses by the interpreter will, in general, follow requests. Sometimes, when the response by the interpreter is not of interest, the response will be omitted.

The response of the interpreter is two-fold; first the type of the result is displayed (in this case INTEGER) and then the meaning or value of the result (in this case 3). This is much like looking up a word in a dictionary. First the part of speach is given (e.g. noun, verb, adjective). Then the meaning of the word is given. Unlike a dictionary, a word always has one meaning. Sway, as with most programming languages, has a small number of simple types: integers, real, numbers, strings, and symbols.

The Sway interpreter, in general, evaluates expressions. As a rule, one signals the end of an expression using the semicolon. In response to an expression, the interpreter determines its value. As will be seen, the Sway interpreter can evaluate much more complicated expressions than the expression 3. But first, let us look at those so-called primitive elements, like 3, whose meanings are the expressions themselves. These primitive values serve as the fundamental building blocks of computer programs.

The things whose values are the things themselves are known as primitives. The primitives of Sway can be categorized by the following types: booleans, 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.

As examples of each of the types, the response to 3 is INTEGER: 3, the response to -4.9 is REAL: -4.9, the response to "hello" is STRING: "hello", and the response to :age is SYMBOL: :age. Let's examine the four types in more detail.

Integers

Integers are numbers without any fractional parts. Examples of integers are:

   sway> 3;
   INTEGER: 3
   sway> -5;
   INTEGER: -5
   sway> 0;
   INTEGER: 0

Integers must begin with a digit or a minus sign. The initial minus sign must immediately be followed by a digit.

Real Numbers

Reals are numbers that do have a fractional part (even if that fractional part is zero!). Examples of real numbers are:

   sway> 3.2;
   REAL: 3.200000
   sway> 4.0;
   REAL: 4.000000
   sway> -5.;
   REAL: -5.000000
   sway> 3.0e-4;
   REAL: 0.000300

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, 3.0*1011 would be entered as 3.0e-11. The 'e' stands for exponent and the 10 is understood, so e-11 means multiply whatver preceeds the e by 1011. However, the Sway interpreter only displays the first six digits after the decimal point so 3.0e-11 would display as:

   sway> 3.0e-11;
   REAL: 0.000000

Don't be fooled. Although the result looks like zero, it is not (the correct value is 0.00000000003).

Strings

Strings are sequences of characters delineated by double quotation marks:

   sway> "hello, world!";
   STRING: "hello, world!"
   sway> "x\nx";
   STRING: "x\nx"
   sway> "\"z\"";
   STRING: "\"z\"" 
   sway> "";
   STRING: ""

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 '\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'. A backslash character must be escaped 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 (6). 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".

Symbols

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

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

Note that 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 two special symbols, :true and :false. In Sway, 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.

Questions

Exercises