Understanding the detailed rules governing how basic expressions (even expressions like “1+2”) is important as it helps us understand programs no matter how big they are.
Let’s look at this expression for an example of such rules:
(+ 2 (* 3 4) (- (+ 1 2) 3))
(
(open parenthesis) and a primitive operation (e.g. +
, -
, /
, *
) is called a primitive call (or a call to a primitive).+
's operands are 2
, (* 3 4)
, (- (+ 1 2)3)
.*
's operands are 3
and 4
.-
's operands are (+ 1 2)
and 3
.+
's operands are 1
and 2
.For a primitive call to be evaluated, its operands must be reduced to values (e.g. (* 3 4)
needs to be eval’d to 12
) before the primitive can be applied.
(+ 2 (* 3 4) (- (+ 1 2) 3))
(+ 2 12 (- 3 3))
(+ 2 12 0)
; Given that the operands have been reduced to values,
; the primitive operation "+" can now be evaluated.
; The result of adding 2, 12, and 0 together is:
> 14
Strings in BSL are denoted by double quotes e.g. "string"
.
The string-append
function (parameters: <str1> <str2> <str3>...<str n>) concatenates strings together.
(string-append "Ada" " " "Lovelace")
> "Ada Lovelace"
Making sure a variable has the data type it should have is important as different data types work differently. e.g. "123"
, a string, can’t be operated on by mathematically unlike 123
, an integer.
123 + 1
> 124
"123" + 1
> +: expected a function call, but there is no open parenthesis before this function
Strings can be sliced via the substring
function (parameters: <string> <start index> <end index>).
(substring "Caribou" 2 4)
> "ri"
(substring "Caribou" 0 3)
> "Car"