Basics
Chapter 2 of 9Names, numbers, text, and yes and no.
Names and values
You give a value a name with =. Comments start with #.
A name never changes after you set it. There is no answer = 43 later on. If you need a different value, you give it a new name.
A name must start with a lowercase letter. Total = 5 is an error (Expected a type here.), because names that start with a capital letter mean something else in Bert.
A capitalized name declares a type. You will meet those in the Variants chapter.
Bert has four basic kinds of value:
| Type | What it holds | Example |
|---|---|---|
Int | Whole numbers | 42, -7 |
Float | Numbers with a decimal point | 3.14 |
Bool | Yes or no | true, false |
String | Text | "hello" |
You never have to write these types down. Bert sees 42 and knows it is an Int. The one exception is a function a module exports, which needs its type written down; see Modules.
Numbers
The usual math works: + - * / %. Multiplication goes before addition, as in school.
Dividing two Ints drops the remainder. If you want decimals, use Floats.
Bert never mixes Int and Float on its own. 1 + 1.5 is an error. Convert first:
Numbers live in modules too. Int.toString(42) gives "42", and Int.fromString goes the other way (more on that in When things go wrong).
Text
A String is text in double quotes. Escapes like \n (new line) and \t (tab) work as you would expect.
Bert has no "Hello, ${name}" shortcut. You join strings with String.concat:
This is also how you put a number into text. Turn the number into text with Int.toString first:
+ does not join text. "Hello, " + name gives the error Operator `+` is not defined for type `String`. Use String.concat.
A few more helpers:
== compares text too, letter by letter:
It works on the other kinds of value you will meet later, too, such as lists.
Functions cannot be compared. f == g is an error.
Yes, no and if
Comparing things gives a Bool:
Bert has no &&, || or !. Use these instead:
if picks one of two values. The result is a value like any other, and the else is always required:
To check more than two cases, chain with else if. This function, sign, tells you whether a number is below, at or above zero. Functions come in the next chapter; for now, read (n) -> as "a recipe that takes n".
Where to find every function
Bert comes with many more functions than this guide shows. They are all listed in the standard library docs. A few worth knowing: List.reverse, List.length, String.startsWith, Int.abs, Int.min and Result.withDefault.
You do not have to remember them: look them up.