2.4 Combinations
A combination is an expression that describes the application of an operator to its operands. For example, numbers can be combined using operations like the sum or product, e.g. \(1 + 2\) and \(1 + 2 \times 3\). The sum and product of numbers are two of the most fundamental operations. Naturally, many other operations can be used, including mathematical functions such as sine (\(\sin\)) or cosine (\(\cos\)). The syntax for functions, however, is slightly different: unlike arithmetic operators, like sum and product that use infix syntax, i.e., the operator appears between the operands, functions use prefix syntax, i.e., they appear before the operands, as in \(\sin 2\). Finally, there are also operators, such as factorial (abbreviated \(!\)) that are used with postfix syntax, as in \(5 !\).
In Julia, a combination can be created using a syntax similar to the one used in mathematics, although with some small differences: functions are always written in prefix syntax but function arguments are grouped with parentheses, as in sin(2). Arithmetic operators can also be treated as functions, as in +(1,2) but they are usually written using infix syntax, as in 1+2. In Julia, an expression is a primitive element or a combination of expressions with an operation. The expression 1+2 is a combination of the two primitive elements 1 and 2 through the primitive procedure +. In the case of 1+2*3, the combination is between the number 1 and the combination 2*3 because Julia follows the same precedence rules used in mathematics. Therefore, the expression 1+2*3 is interpreted as 1+(2*3).
2.4.1 Exercises 2
2.4.1.1 Question 4
Explain the acronym REPL.
2.4.2 Evaluating Combinations
The evaluator determines a combination’s value by applying the procedure specified by the operator to the value of the operands. The value of an operand is designated as the argument of the procedure. The value of the combination 1+2*3 is the result of adding the value of 1 to the value of 2*3. As we have already seen, the value of 1 is \(1\) and 2*3 is a combination whose value is the result of multiplying 2 by 3, which is \(6\). Finally, by summing \(1\) with \(6\) we get \(7\).
The following interaction with Julia’s REPL demonstrate the evaluation of a few combinations:
> 1+2
3
> 12345+54321
66666
> 12345*54321
670592745
> 1/2
0.5
> 1+2*3
7
> (1+2)*3
9
2.4.3 Strings
Chains of characters (also called Strings) are another type of primitive data. A character is a letter, a digit or any kind of graphic symbol, including non-visible graphic symbols like blank spaces, tabs and others. A string is specified by a character sequence between quotations marks. Just like with numbers, the value of a string is the string itself:
> "Hi"
"Hi"
> "I am my own value"
"I am my own value"
Since a string is delimited by quotation marks, how can we create a string that contains quotation marks? For this and other special characters, there is one particular character that Julia interprets differently: when the character \ appears in a string, it tells Julia that the next characters must be evaluated in a special way. For example, to create the following string:
John said "Good morning!" to Peter.
We must write:
"John said \"Good morning!\" to Peter."
The character \ is called an escape character and allows the inclusion of characters in strings that would otherwise be difficult to input. This table shows examples of other special characters that need to be escaped.
Some valid escape characters in Julia.
As it happens with numbers, there are countless operators to manipulate strings. For example, to concatenate multiple strings we can use the * operator. The concatenation of several strings produces a single string with all the characters of those strings, in the same order:
> "1"*"2"
"12"
> "one"*"two"*"three"*"four"
"onetwothreefour"
> "I"*" "*"am"*" "*"a"*" "*"string"
"I am a string"
> "And I"*" am "*"another"
"And I am another"
To know how many characters there are in a string we have the length operator:
> length("I am string")
11
> length("")
0
Note that quotation marks define the string boundaries and are not considered part of the string itself. Besides strings and numbers, Julia has other kinds of primitive elements that will be addressed later.