Examples
Explore Roc through small, runnable examples.
Getting started
- Hello, World! — Print a greeting with a minimal Roc application.
- Multi-line Comments — Write comments across several lines and learn why Roc uses # for each line.
- 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.
- Pattern Aliases — Name an entire matched pattern while binding its individual parts.
- Nominal and Opaque Types — Define distinct types and control access to their underlying representation.
- Open Records — Accept records with additional fields while requiring the fields a function needs.
- Nested Types — Group related types inside a type module.
- Recursive Types — Build expression trees with recursive and mutually recursive types.
- Polymorphism — Write reusable functions with where constraints and shared constraint aliases.
- Custom Inspect — Customize how opaque values appear when inspected, including hiding a secret.
- Custom Literals — Create values of your own types from number literals, quoted strings, and interpolated strings.
Collections and iteration
- Basic Dict Usage — Create a dictionary and look up, insert, and remove entries.
- Sets — Create sets and combine them with union, intersection, and difference.
- Loops — Use for, while, and local variables, and compare loops with fold.
- Looping Effectful Functions — Repeat an effectful operation by reading and summing numbers until input ends.
- Streams and for! — Consume effectful stream pipelines with for! and create a custom stream for reading file lines.
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.
- Wrapping and Saturating Arithmetic — Choose wrapping or saturating arithmetic when values exceed numeric limits.
- Error Handling Real World (Try) — Combine error handling across several operations in a command-line program.
Parsing and encoding
- String Capture Patterns — Capture filenames and parse numeric IDs from paths with string patterns.
- JSON — Parse JSON data into a typed Roc record.
- Advanced Parsing — Parse optional fields, defaults, and camelCase JSON, and work with other formats.
- 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
- Paths — Build and inspect file paths with the path package.
- Random Numbers — Generate reproducible pseudorandom numbers with the roc-random package.
- Unicode Text — Work with graphemes, Unicode scalars, word boundaries, and casing.
- 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.
roc-ray
- Drawing with RocRay — Draw an interactive graphical application with the roc-ray platform.
Each example’s page lists the Roc version used to validate it.
Their source comes from roc-lang/examples at baa8add.