A tour of the language
Each snippet below uses syntax that matches the current compiler and passing
tests. Bindings are implicitly typed; make appears only where a
specific type or container is required.
1 · Basic values
a: 10
b: 20
print a + b
dbl: 3.14159
print dbl
flag: true
ch: #"A"
print ch
2 · Functions and implicit return
The last expression in a body is the result unless an earlier
return exits.
add: func [
"Add two signed 32-bit integers."
a [i32!] "Left operand."
b [i32!] "Right operand."
return: [i32!] "The sum."
] [
a + b
]
print add 7 8
3 · if, either, block-valued branches
x: either true [1] [0] ; a value needs both branches
print x
choose: func [
"Pick a number by branch: 12 when the flag is true, otherwise 23."
flag [logic!] "Selects which branch runs."
return: [i32!] "12 when flag is true, otherwise 23."
] [
either flag [
a: 10
a + 2
] [
b: 20
b + 3
]
]
print choose true
print choose false
4 · Short-circuit boolean expressions
mark: func [
"Print 9 and return true, so a caller can see whether it ran."
return: [logic!] "Always true."
] [
print 9
return 1 = 1
]
first: make logic! all [0 = 1 mark]
second: make logic! any [0 = 1 mark]
print first
print second
5 · Structs, vectors & indexed paths
Field access uses /field; variable indexing uses
/:idx.
voice!: make struct! [phase: f64! active: logic!]
voices: make [#mutable vector! [voice! 2]] []
idx: 1
voices/:idx/phase: 0.5
voices/:idx/active: true
print voices/:idx/phase
print voices/:idx/active
6 · Strings, blocks, dicts & slices
buf: make [#flexible [string! 16]] none
append buf "hello"
append buf ", world"
print freeze buf
nums: make [#flexible [array! [i64!] 8]] none
append nums 10
append nums 20
frozen: freeze nums
print length? frozen
counts: make [#mutable dict! [string! i32!]] [string! i32!]
get-or-put counts "ok" 1
print pick counts "ok"
7 · Borrowing
greet: func [
"Print a borrowed string."
:name [string!] "The borrowed string to print; the caller keeps it."
return: [none!] "Nothing."
] [
print name
]
msg: make string! "hello"
greet :msg
print msg
7b · Sharing, and #unique moves
Strings, blocks and vectors are shared by default: an assignment adds a
reference and both bindings stay valid. #unique opts a series back into
move-only ownership.
greeting: "Hello"
echo: greeting
print echo
print greeting ; ok: both are live
token: make [#unique string!] "secret"
owner: token
print token ; B3000: 'token' used after move
8 · fn-ptr! & closures
add: func [
"Add two signed 32-bit integers."
x [i32!] "Left operand."
y [i32!] "Right operand."
return: [i32!] "The sum."
] [
return x + y
]
callback: make fn-ptr! add
print callback 3 4
base: 100
bump: make fn-ptr! func [
"Add the captured base to a value."
#capture [base]
a [i32!] "The value to offset."
return: [i32!] "The value plus the captured base."
] [
a + base
]
print bump 7
9 · parse
print parse "abcd" ["ab" "cd"]
print parse "cd" ["ab" | "cd"]
chs: make bitset! charset ":/#?"
print parse "abc?" [thru chs]
print parse "a" [ahead "a" "a" end]
More on the PARSE page.
10 · go
demo: func [
"Spawn a task, then yield so the task gets to run."
] [
print "main"
go [print "task"]
sleep 10 ; yield, so the task gets to run
]
demo
go is only allowed inside a function body.
11 · fsm! and send
TrafficLight!: make fsm! [
events: [go ["Go"] slow ["Slow"] stop ["Stop"]]
red [go => green]
green [slow => yellow]
yellow [stop => red]
]
light: make [#mutable TrafficLight!] TrafficLight!/red
send light go []
send light slow []
More on the State Machines page.
11b · fst!, transduce/lazy & take
Token!: make sum! [
ident
num [value: i32!]
]
Lexer!: make fst! [
emits: Token!
fields: [count: i32!]
scanning [
["A"] => scanning [count: count + 1 emit Token!/num count]
]
finished []
]
drive: func [
"Pull one value from a lazy lexer and print a code for the result variant."
] [
lex: transduce/lazy Lexer! "AAA"
result: take lex
match result [
ok [v] [print 1]
need-more [] [print 8]
done [] [print 9]
fail [err] [print 7]
]
]
12 · Ports & file I/O
fp: make port! file/open %core.reb
either none? fp [print 0] [print 1]
Borrowed and consuming operations follow the port! model:
file/take :fp borrows the port and file/close fp consumes it.
13 · FFI
math: foreign <stdlib.h> [
abs: "abs" [i32! return: i32!]
]
x: math/abs -7
print x
More on the FFI & Systems page.
14 · Modules
Recoil [
Type: module
Module: tests/math-module-demo
Imports: [
std/math/core [abs-value clamp-value]
]
Exports: []
]
print abs-value [i32!] -7
print clamp-value [i32!] 10 0 5
15 · Parse + FSM integration
parse "GET /path" [
"GET" (send conn got-method [])
" "
thru end
]
either state? conn Conn!/reading-body [
parse "payload" [thru end (send conn got-body [])]
] [
print "still waiting for method"
]
16 · Tasks & channels
run: func [
"Send two values through a channel from a spawned task and print them as they are received."
return: [none!] "Nothing."
] [
ch: make channel! [i32! 4]
go [
send ch 1
send ch 2
]
print receive ch ; 1
print receive ch ; 2
]
run
17 · Atomics
counter: make [#atomic i32!] 0
atomic-add counter 5
old: atomic-exchange counter 7
print old ; 5
print counter ; 7
More from the test suite
Browse a selection of complete programs in the repository: