Chapter 1: First steps

Lasagna

  • functions
  • Int
  • type annotations

A function's last expression is its result; there is no return needed for the normal case. Parameters can carry type annotations like layers: Int, and Gleam's type checker infers the rest.

Functions without pub are private to the module. Calling one looks like you would expect: preparation_time_in_minutes(2).

Open in playground
def expected_minutes_in_oven() {
  40
}

def remaining_minutes_in_oven(actual_minutes_in_oven: Int) {
  expected_minutes_in_oven() - actual_minutes_in_oven
}

def preparation_time_in_minutes(layers: Int) {
  2 * layers
}

def total_time_in_minutes(layers: Int, actual_minutes_in_oven: Int) {
  preparation_time_in_minutes(layers) + actual_minutes_in_oven
}

def alarm() {
  "Ding!"
}

pub def main() {
  echo expected_minutes_in_oven()
  echo remaining_minutes_in_oven(29)
  echo preparation_time_in_minutes(2)
  echo total_time_in_minutes(2, 3)
  echo alarm()
}

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