Roc is a fast, friendly, functional programming language. This page lets you try small programs directly in the browser. Every box below is a live editor backed by the real Roc compiler compiled to WebAssembly.
Edit the code, hit Run ▶, and see the output appear below.
A Roc program starts with a main! function. The
! at the end of the name means it's an
effectful function, it can run side effects like printing.
echo! prints a line of text to the output.
main! = |_| {
echo!("Hello, Roc!")
Ok({})
}
You declare constants with = inside a block.
Constants are immutable, they can't be reassigned. Strings support
interpolation with ${…}.
main! = |_| {
name = "Rocco"
greeting = "Ciao, ${name}!"
echo!(greeting)
Ok({})
}
Functions are ordinary values in Roc. A pure function (no side
effects) has a plain name; an effectful function ends with
!. The last expression in a block is its return value.
main! = |_| {
square = |n| n * n
echo!("5 squared is ${square(5).to_str()}")
Ok({})
}
Records group named fields together. Access a field with dot
notation. You can "update" a record with
{ ..record, field: new_value }.
main! = |_| {
point = { x: 3, y: 4 }
echo!("x = ${point.x.to_str()}")
echo!("\n")
echo!("y = ${point.y.to_str()}")
Ok({})
}
Use match to branch on a value's shape. List patterns
can destructure the first elements with [first, ..] and
the default _ branch catches everything else.
main! = |_| {
animals = ["bird", "crab", "lizard"]
points = match animals {
["bird", "crab", "lizard"] => 10
["bird", ..] => 5
_ => 0
}
echo!("Points: ${points.to_str()}")
Ok({})
}
In Roc, if is an expression and it evaluates to a
value. This means you can use it anywhere an expression is expected,
like assigning its result to a constant.
main! = |_| {
name = ""
greeting = if name == "" {
"Hello, stranger!"
} else {
"Hello, ${name}!"
}
echo!(greeting)
Ok({})
}
Write your own Roc program and run it!
main! = |_| {
echo!("Write your code here!")
Ok({})
}