Examples
Explore Roc through small, runnable examples.
Getting started
- Hello, World! — Print a greeting with a minimal Roc application.
- FizzBuzz — Use conditions and arithmetic to solve the classic FizzBuzz exercise.
- All Syntax in 1 File — Explore the language syntax in one annotated file.
Types and functions
- Tuples — Group values of different types into tuples and access their elements.
- Pattern Matching on Lists — Match list shapes and bind their elements to names.
- Custom Inspect — Customize how opaque values appear when inspected, including hiding a secret.
Collections and iteration
- Basic Dict Usage — Create a dictionary and look up, insert, and remove entries.
- Looping Effectful Functions — Repeat an effectful operation by reading and summing numbers until input ends.
Error handling
- Error Handling Basic (Try) — Represent success and failure with Try and handle both outcomes.
?Operator Desugaring — See how the ? operator expands into explicit error handling.- Safe Math — Handle arithmetic overflow and other numeric errors explicitly.
- Error Handling Real World (Try) — Combine error handling across several operations in a command-line program.
Parsing and encoding
- JSON — Parse JSON data into a typed Roc record.
- Encoding & Decoding — Define custom encoders and parsers for a nominal type.
- Record Builder — Build a date parser by combining field parsers with record builder syntax.
Programs and algorithms
- Least Squares — Find the smallest integer whose square exceeds the previous square by more than 1,000.
- Towers of Hanoi — Solve the Towers of Hanoi puzzle with recursion.
- Graph Traversal — Represent a graph and traverse its connected nodes.
Organizing code
- Import Files — Embed text or binary files in an executable at compile time.
- Multiple Roc Files — Split an application into modules across multiple files.
- Import from Directory — Import a module from a subdirectory.
- Import Package from Module — Pass a package dependency from an application to one of its modules.
Packages
- Random Numbers — Generate reproducible pseudorandom numbers with the roc-random package.
- Parser — Compose a parser that counts letters using the roc-parser package.
Platforms
basic-cli
- CLI Args Simple — Read arguments passed to a command-line application.
- CLI Args File — Open a file named by a command-line argument and read its first line.
- Terminal Commands — Run an external command and work with its output.
- Snake Game — Build a playable Snake game with an effectful game loop.
These examples are built and tested with Roc nightly-2026-10-09-258ab27, unless noted otherwise.
Their source comes from roc-lang/examples at 0457fea.