Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Beginner programs

These programs live in the repository under examples/beginner/.

  • Scripts: romit run examples/beginner/hello.rmt
  • Specs with tests: romit test examples/beginner/<name>.rmt

records.rmt

Records, maps, and list transforms: with name "Ada", age 36, where, each, sorted by, grouped by, map "tea" to 3, or else.

match_and_errors.rmt

enum, match with values and comparisons, fail with try / if it fails, and counting down by a step.

quiz.rmt

ask for input: romit run examples/beginner/quiz.rmt, or type answers into Input for ask in the playground.

lists.rmt

groceries = list "milk", "eggs"
add "bread" to groceries
names = join groceries, ", "
say "You need {count of groceries} things: {names}"

for each item in groceries
  say "- {upper item}"

hello.rmt

say "Hello, world!"

hello_world.rmt

spec HelloWorld version 1
about "Say hello — the smallest Romit program."

output Greet
  message text

event Run

on Run
  rule say_it
    when true
    emit Greet with message "Hello, world!"

test says_hello
  send Run
  expect emitted Greet with message "Hello, world!"

print is a full alias of say in scripts:

print "Hi"
set name to "Romit"
print "Hello, " name
say name
romit run examples/beginner/print_alias.rmt

variables.rmt

Top-level script variables with set and say:

set total to 3 times 4 plus 2
say "Total: " total
set x to 5
set x to x plus 1
say x
romit run examples/beginner/variables.rmt

assign_equals.rmt

Same semantics using = at the start of a line (x=5 and x = 5):

x=5
total = 3 times 4 plus 2
name = "Ada"
x = x plus 1
say "Hi " name
say "Total: " total
say x
romit run examples/beginner/assign_equals.rmt

write_what_you_mean.rmt

Used in Why Romit — a long English prompt versus one runnable line:

say "Hello from Romit"
romit run examples/beginner/write_what_you_mean.rmt

sum_fn.rmt

Script function with the preferred sum: header and a say call (no colon at the call site):

sum: a, b
  give a plus b

say sum 3, 4
romit run examples/beginner/sum_fn.rmt

fn_aliases.rmt

The same definition with fn and function (interchangeable with name:):

function add a, b
  give a plus b

fn mul a, b
  give a times b

say add 2, 3
say mul 4, 5
romit run examples/beginner/fn_aliases.rmt

operators.rmt

Symbol and word operators (plus, is at least, remainder, …):

romit run examples/beginner/operators.rmt

control_flow.rmt

if, otherwise if, otherwise, while, and for each in a script.

romit run examples/beginner/control_flow.rmt

factorial.rmt

Recursion with give and comparison words:

romit run examples/beginner/factorial.rmt

greet_default.rmt

Default parameter value with is:

romit run examples/beginner/greet_default.rmt

calculator.rmt

See the Calculator tutorial.

spec Calculator version 1
about "Multiply and add in one line — no buttons required."

state
  answer int is 0
event Evaluate

on Evaluate
  rule sample
    when true
    set answer to 3 times 4 plus 2

test two_times_three_plus_two
  send Evaluate
  expect answer is 14

fibonacci.rmt

spec Fibonacci version 1
about "Small Fibonacci answers without recursion — Stage 2 adds real functions."

state
  result int is 0

event Compute with n int

on Compute with n if n is 0
  rule zero
    when true
    set result to 0

on Compute with n if n is 1
  rule one
    when true
    set result to 1

on Compute with n if n is 10
  rule ten
    when true
    set result to 55

test fib_of_10
  send Compute with n 10
  expect result is 55

test fib_of_0
  send Compute with n 0
  expect result is 0

test fib_of_1
  send Compute with n 1
  expect result is 1

todo_list.rmt

Walkthrough: Todo list tutorial.

spec TodoList version 1
about "One task at a time: add text, then mark it done."

state
  task_text text is ""
  task_done bool is false

event Add
event MarkDone

output TaskAdded
  title text

output TaskCompleted
  title text

on Add with title
  rule create
    when not task_done or task_text is ""
    set task_text to title
    set task_done to false
    emit TaskAdded with title title

on MarkDone
  rule finish
    when task_text is not "" and not task_done
    set task_done to true
    emit TaskCompleted with title task_text

test add_then_done
  send Add with title "Buy milk"
  expect task_text is "Buy milk"
  expect emitted TaskAdded with title "Buy milk"
  send MarkDone
  expect task_done is true
  expect emitted TaskCompleted with title "Buy milk"

traffic_light.rmt

Walkthrough: Traffic light tutorial.

spec TrafficLight version 1
about "A traffic light that cycles red → green → yellow."

enum Color red green yellow

state
  light Color is red

event Tick

output ShowLight
  color text

machine light
  red to green
  green to yellow
  yellow to red

on Tick if light is red
  rule advance
    when true
    set light to green
    emit ShowLight with color green

on Tick if light is green
  rule go_yellow
    when true
    set light to yellow
    emit ShowLight with color yellow

on Tick if light is yellow
  rule go_red
    when true
    set light to red
    emit ShowLight with color red

invariant only_known_colors
  light is red or light is green or light is yellow

test red_to_green
  send Tick
  expect light is green
  expect emitted ShowLight with color green

test full_cycle
  given light is green
  send Tick
  expect light is yellow
  send Tick
  expect light is red