Skip to content
Menu
Contents

Basics

Chapter 2 of 9

Names, numbers, text, and yes and no.

Names and values

You give a value a name with =. Comments start with #.

# A name for a value
answer = 42
greeting = "hello"

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:

TypeWhat it holdsExample
IntWhole numbers42, -7
FloatNumbers with a decimal point3.14
BoolYes or notrue, false
StringText"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.

2 + 3 * 4      # => 14
7 / 2          # => 3   (whole numbers stay whole)
7 % 2          # => 1   (the remainder)
9.5 * 2.0      # => 19.0

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:

Int.toFloat(1) + 1.5    # => 2.5

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:

String.concat("Hello, ", "Bert")    # => "Hello, Bert"

This is also how you put a number into text. Turn the number into text with Int.toString first:

n = 7
message = String.concat("n is ", Int.toString(n))    # => "n is 7"

+ does not join text. "Hello, " + name gives the error Operator `+` is not defined for type `String`. Use String.concat.

A few more helpers:

String.split("a,b,c", ",")           # => ["a", "b", "c"]
String.join(["a", "b"], "-")         # => "a-b"
String.trim("  hi  ")                # => "hi"
String.contains("bertram", "ert")    # => true

== compares text too, letter by letter:

"a" == "a"      # => true
"a" == "b"      # => false

It works on the other kinds of value you will meet later, too, such as lists.

Note

Functions cannot be compared. f == g is an error.

Yes, no and if

Comparing things gives a Bool:

18 >= 18     # => true
3 == 4      # => false

Bert has no &&, || or !. Use these instead:

Bool.and(true, false)    # => false
Bool.or(true, false)     # => true
Bool.not(true)           # => false

if picks one of two values. The result is a value like any other, and the else is always required:

label = if 5 > 3 then "big" else "small"
# => "big"

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".

sign = (n) ->
  if n < 0 then "negative"
  else if n == 0 then "zero"
  else "positive"
sign(0)    # => "zero"

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.