   Combining Literals Contents

# Combining Literals

Like the literals themselves, combinations of literals 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 Scam, presenting the interpreter with the expression:

```    (* 8 7)
-> 56
```

The multiplication sign * is known as an operator, as it operates on the 8 and the 7, producing an equivalent literal value. As with all LISP/Scheme-like languages, operators like `*` are true functions and thus prefix notation is used in function calls.

The 8 and the 7 in the above expression 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 of two operands, 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.

We separate operators from their operands by spaces, tabs, or newlines, collectively known as whitespace.1

Scam always takes in an expression and returns an equivalent literal expression (e.g., integer or real). All Scam operators are variadic, meaning they operate on exactly on any number of operands:

```    (+ 1 2 3 4 5)
-> 15
```

## Numeric operators

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

```    (+ 2 3)
-> 5

(+ 1.9 3.1)
-> 5.000000
```

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. Things get more interesting when you add things having different types. Adding an integer and a real (in any order) always yields a real.

```    (+ 2 3.3)
-> 5.300000

(+ 3.3 2)
-> 5.300000
```

Adding an string to an integer (with an augend integer) yields an error; the types are not `close' enough, like they are with integers and reals:

```    (+ 2 "hello")
-> EXCEPTION: generalException
wrong types for '+': INTEGER and STRING
```

In general, when adding two things, the types must match or nearly match.

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

```    15 / 2
```

If one asked the Scam interpreter to perform this task, the result will not be 7.5, as expected, but rather 7, as the division operator performs integer division:

```    (/ 15 2)
-> 7
```

However, we wish for a real result, we can convert one of the operands to a real, as in:

```    (/ (real 15) 2)
-> 7.500000
```

Note that Scheme would produce the rational number (15)/(2) in this case. Scam does not have rationals, but they can be added to the language if one desires more Scheme compatibility.

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)
-> 4
```

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)
-> #t
```

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 Scam interpreter will respond that, indeed, it is true.

## Combining strings

To concatenate strings together, one uses the string+ operator. Like `+` and `-,` string+ is variadic; we can concatenate any number of strings at one time. Strings are concatenated from left to right. For examle, the expression:

```    (string+ "a" "b" "c")
```

produces the new string `"abc"`. Note that the strings passed to string+ are unmodified.

## Comparing things

Remember the Boolean literals, #t and #f? We can use the Boolean comparison operators to generate such values. For example, we can ask if 3 is less than 4:

```    (< 3 4)
-> #t
```

Evaluating this expression shows that, indeed, 3 is less than 4. If it were not, the result would be #f. 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).

```    (< 1 2 3)
-> #t

(< 1 2 2)
-> #f
```

Any Scam 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 #f. Conversely, comparing different types with != will yield #t (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

We can combine comparisons with the Boolean logical connectives and and or:

```    (and (< 3 5) (< 4 5))
-> #t

(or (< 3 4) (< 4 5))
-> #t

(and (< 3 4) (< 5 4))
-> #f

(or (< 3 4) (< 5 4))
-> #t
```

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 and or is illustrated in the last two interactions. Since only one expression is true (the latter expression being false) only the or operator yields a true value. If both expressions are false, both `and` and `or` returns false.

There is one more Boolean logic operation, called not. It simply reverses the value of the given expression.

```    (not (and (< 3 4) (< 4 5)))
-> #f
```

lusth@cs.ua.edu   Combining Literals Contents