Chapter 1: First steps

Pac-Man rules

  • Bool
  • and / or / not
  • imports

Bea spells boolean logic as words: and, or and not. They become Gleam's &&, || and !, and they short-circuit the same way.

import gleam/io brings in the standard library's IO module. You can skip imports for the standard library entirely: Bea adds list, string, int and friends automatically when you use them.

Open in playground
import gleam/io

def eat_ghost(power_pellet_active: Bool, touching_ghost: Bool) -> Bool {
  touching_ghost and power_pellet_active
}

def score(touching_power_pellet: Bool, touching_ghost: Bool) -> Bool {
  touching_ghost or touching_power_pellet
}

def lose(power_pellet_active: Bool, touching_ghost) -> Bool {
  touching_ghost and not power_pellet_active
}

pub def main() {
  echo eat_ghost(True, False)
  echo score(False, True)
  echo lose(True, True)
  io.println("Done!")
}

An exercism exercise from the Gleam track, solved in Bea: beacode/src/e3_pacman_rules.bea