Skip to content

A small program

When a formula isn’t enough, a definition can hold a program: a few lines worked out in order, drawn with a bar down their left side.

grade(U) :=
  "OK" if U <= 1
  "fails" otherwise
grade(0.8) = ?
grade(1.2) = ?
As text
grade(U) :=
  "OK" if U <= 1
  "fails" otherwise
grade(0.8) = ?
grade(1.2) = ?
Open this example · Download .bbm

Type the definition’s name and :, then ]. The value becomes a program with two empty lines. In each line:

  • } makes an if line: the value first, then the condition after if.
  • Type otherwise and a space on a new line for the line that runs when no if above it did.
  • ] adds another line below the one you are on.

The toolbar’s Programming palette has a button for each, with its key in the tooltip.

The program’s value is the value of the last line it runs.

{ makes a local assignment, drawn ←. Local names exist only inside the program; changing one never changes the sheet’s variables.

fib(n) :=
  a <- 0
  b <- 1
  for k in 1..n
    t <- a + b
    a <- b
    b <- t
  a
fib(10) = ?
As text
fib(n) :=
  a <- 0
  b <- 1
  for k in 1..n
    t <- a + b
    a <- b
    b <- t
  a
fib(10) = ?
Open this example · Download .bbm

Type for and a space for a for loop, while and a space for a while loop. A for loop runs over a range (1..n) or over the elements of a vector:

peak(v) :=
  k <- 0
  for j in 0..last(v)
    k <- j if v[j] > v[k]
  k
peak([3; 9; 4]) = ?
As text
peak(v) :=
  k <- 0
  for j in 0..last(v)
    k <- j if v[j] > v[k]
  k
peak([3; 9; 4]) = ?
Open this example · Download .bbm
halvings(x) :=
  n <- 0
  while x > 1
    x <- x/2
    n <- n + 1
  n
halvings(1000) = ?
As text
halvings(x) :=
  n <- 0
  while x > 1
    x <- x/2
    n <- n + 1
  n
halvings(1000) = ?
Open this example · Download .bbm

A loop can build an array:

squares(n) :=
  for k in 0..n - 1
    v[k] <- k^2
  v
squares(4) = ?
As text
squares(n) :=
  for k in 0..n - 1
    v[k] <- k^2
  v
squares(4) = ?
Open this example · Download .bbm

return ends the program with a value. break leaves a loop, and continue goes on to its next turn.

f(x) :=
  return 0 if x < 0
  sqrt(x)
f(-4) = ?
f(9) = ?
As text
f(x) :=
  return 0 if x < 0
  sqrt(x)
f(-4) = ?
f(9) = ?
Open this example · Download .bbm

try a on error b is a, or b when working out a fails. It works anywhere, not only in programs:

safe(x) := try 1/x on error 0
safe(0) = ?
safe(4) = ?
As text
safe(x) := try 1/x on error 0
safe(0) = ?
safe(4) = ?
Open this example · Download .bbm

Careful with a for loop over 1..n when n can be 0: a range counts down as well as up, so 1..0 runs twice, for 1 and then 0.