Jump to content

The Sway Reference Manual/Combining Primitives

From Wikibooks, open books for an open world

Like the primitives themselves, combinations of primitives are also expressions. For example, suppose you have forgotten your times table and aren't quite sure whether 8 times 7 is 54 or 56. We can ask Sway, presenting the interpreter with the expression:

   sway> 8 * 7;
   INTEGER: 56

As before, the semicolon signals the end of the expression. The multiplication sign * is known as an operator, as it operates on the 8 and the 7, producing an equivalent primitive value. The 8 and the 7 are known as operands. It seems that the actual names of various operands are not being taught anymore, so for nostalgia's sake, here they are. The operand to the left of the multiplication sign (in this case the 8) is known as the multiplicand. The operand to the right (in this case the 7) is known as the multiplier. The result is known as the product.

The operands of the other basic operators have special names too. For addition, the left operand is known as the augend and the right operand is known as the addend. The result is known as the sum. For subtraction, the left operand is the minuend, the right the subtrahend, and the result as the difference. Finally for division (and I think this is still taught), the left operand is the dividend, the right operand is the divisor, and the result is the quotient.

In general, operators are separated from their operands by spaces, tabs, or newlines, collectively known as whitespace[1].

It would be an error to enter the expression 8*7; as the times operator does not have the requisite whitespace.

In fact, Sway always takes in an expression and displays an equivalent primitive expression (e.g., integer or real). All Sway operators are binary, meaning they operate on exactly two operands. We first look at the numeric operators.

Numeric operators

[edit | edit source]

If it makes sense to add two things together, you can probably do it in Sway using the + operator. For example:

   sway> 2 + 3;
   INTEGER: 5
   
   sway> 1.9 + 3.1; 
   REAL: 5.00000000000
   
   sway> "hello" + "world"
   STRING: helloworld
   

One can see that if one adds two integers, the result is an integer. If one does the same with two reals, the result is a real. The same is true for adding strings to strings. Things get more interesting when you add things having different types. Adding an integer and a real (in any order) always yields a real.

   sway> 2 + 3.3;
   REAL: 5.30000000000
   
   sway> 3.3 + 2;
   REAL: 5.30000000000
   

Adding an integer and a string (with an augend integer) yields an integer. The addend string is interpreted as an integer, if possible:

   sway> 2 + "3";
   INTEGER: 5
   
   sway> 2 + "3.3";
   INTEGER: 5
   
   sway> 2 + "hello";
   INTEGER: 2

Adding an integer and a string (with an augend string) yields an integer. The addend integer is interpreted as string:

   sway> "hello" + 2;
   INTEGER: "hello2"
   
   sway> "3" + 2;
   STRING: "32"
   

Subtraction, multiplication, and division of numbers follow the same rules as addition. However, these operators, as defined, do not work for strings[2].

Of special note is the division operator with respect to integer operands. Consider evaluating the following expression:

   14 / 2

If one asked the Sway interpreter to perform this task, the result would be 7, as expected. However, if asked the interpreter to evaluate the expression...

   14 / 5

the result would be 2, not 2.8. Consistent with the rule above, integer division always returns an integer and if there is a fractional part, it is discarded. Note that all six of the following expressions evaluate to 2.8:

   14 / 5.0
   14.0 / 5
   14.0 / 5.0
   14 / real(5)
   real(14) / 5
   real(14) / real(5)

The last three illustrate the use of a conversion function. In the above cases, the integers are converted to reals before the division occurs.

The complement to integer division is the modulus operator %. While the result of integer division is the quotient, the result of the modulus operator is the remainder. Thus

   14 % 5

evaluates to 4 since 4 is left over when 5 is divided into 14. To check if this is true, one can ask the interpreter to evaluate:

   14 / 5 * 5 + (14 % 5) == 14

This complicated expression asks the question 'is it true that the quotient times the divisor plus the remainder is equal to the original dividend?'. The Sway interpreter will respond that, indeed, it is true. The reason for the parentheses is due to a lack of precedence among the Sway arithmetic operators and is explained in more detail in the next chapter.

About whitespace

[edit | edit source]

Whitespace (tab, newline, and space) in a computer program has two purposes. The first is to make expressions in the language pleasing to the eye. The second is whitespace often signals the end of a primitive. Unlike many programming languages, Sway uses whitespace to signal the end of operators. Thus, 8 * 7 is interpreted as two numbers separated by the * operator while 8*7 (no spaces) will be considered a 'bad number'. We will learn about variables in a bit.

Comparing things

[edit | edit source]

Remember the Boolean primitives, :true and :false? We can use the boolean comparison operators to generate such values. For example, we can ask if 3 is less than 4:

   sway> 3 < 4;
   SYMBOL: :true

The interpreters response says that, indeed, 3 is less than 4. If it were not, the interpreter would respond with :false. Besides < (less than), there are other Boolean comparison operators: <= (less than or equal to), > (greater than), >= (greater than or equal to), == (equal to), and != (not equal to).

Besides integers, we can compare reals with reals and strings with strings using the less-than-or-greater-than-like operators. In general, it is illegal to compare integers or reals with strings.

Any Sway type can be compared with any other type with the == and != comparison operators. If an integer is compared with a real with these operators, the integer is converted into a real before the comparison is made. In other cases, comparing different types with == will yield a value of :false. Conversely, comparing different types with != will yield :true (the exception, as above, being integers compared with reals). If the types match, == will yield true only if the values match as well. The operator != behaves accordingly.

Combining comparisons

[edit | edit source]

We can combine comparisons with the Boolean logical connectives && (and) and || (or).

   sway> 3 < 4 && 4 < 5;
   SYMBOL: :true
   
   sway> 3 < 4 || 4 < 5;
   SYMBOL: :true
   
   sway> 3 < 4 && 5 < 4;
   SYMBOL: :false
   
   sway> 3 < 4 || 5 < 4;
   SYMBOL: :true

The first interaction asks if both the expression 3 < 4 and the expression 4 < 5 are true. Since both are, the interpreter responds with :true. The second interaction asks if at least one of the expressions is true. Again, the interpreter responds with :true. The difference between && and || is illustrated in the last two interactions. Since only one expression is true (the latter expression being false) only the || operator yields a true value.

There is one more Boolean logic operation, called not. It simply reverses the value of the expression to which it is attached. The not operator can only be called as a function (since it is not a binary operator). Since you do not yet know about functions, I'll show you what it looks like but won't yet explain its actions.

   sway> not(3 < 4 && 4 < 5);
   SYMBOL: :false
   
   sway> not(3 < 4 || 4 < 5);
   SYMBOL: :false
   
   sway> not(3 < 4 && 5 < 4);
   SYMBOL: :true
   
   sway> not(3 < 4 || 5 < 4);
   SYMBOL: :false

Note that we attached not to each of the previous expressions involving the logical connectives. Note also that the response of the interpreter reversed from before in each case.

Footnotes

[edit | edit source]
  1. Computer Scientists, when they have to write their annual reports, often refer to the things they have done and are reporting on as darkspace. It's always good to have a lot of darkspace!
  2. This does not mean you can never subtract strings. Later you will learn how to override Sway's minus operator so that you can subtract strange things to your heart's content.


Primitives · Precedence and Associativity