Skip to content

Instantly share code, notes, and snippets.

@osbre
Created March 15, 2026 15:03
Show Gist options
  • Select an option

  • Save osbre/67b4733dab8bd8bb5dd0c7ec45aa1636 to your computer and use it in GitHub Desktop.

Select an option

Save osbre/67b4733dab8bd8bb5dd0c7ec45aa1636 to your computer and use it in GitHub Desktop.
Minimal LLMs context of the Gleam language based on https://tour.gleam.run/everything
# Gleam — Minimal Context Guide for LLMs
## Core Language Traits
* **Functional**, **immutable**, **statically typed**.
* **No null, exceptions, implicit conversions, loops, macros, or typeclasses.**
* Flow control via **pattern matching (`case`)** and **recursion**.
* Errors handled with **`Result(value, error)`**.
* Functions always return a value (often `Nil`).
---
# Modules & Imports
```gleam
import gleam/io
import gleam/string as text
import gleam/io.{println}
import gleam/option.{type Option, Some, None}
```
Rules:
* Module name = file path.
* Use `as` to rename modules.
* Use `{}` for **unqualified imports**.
* Types can be imported with `type`.
---
# Program Entry
```gleam
import gleam/io
pub fn main() {
io.println("Hello")
}
```
* `pub` → public
* `fn` → function
* last expression = return value
---
# Variables
```gleam
let x = 5
let y: Int = 5
let _unused = 10
```
Rules:
* immutable
* snake_case
* `_name` suppresses unused warning
---
# Basic Types
### Int
```
+ - * / %
> < >= <=
```
### Float
```
+. -. *. /.
>. <. >=. <=.
```
### Bool
```
True False
&& || !
```
### String
```
"hello"
"hello" <> "world"
```
Escapes:
```
\" \\ \n \t \r
\u{XXXX}
```
---
# Collections
## List
```gleam
let xs = [1,2,3]
[-1, ..xs]
```
Type:
```
List(Int)
```
Pattern match:
```
[]
[first, ..rest]
```
---
## Tuple
```
#(1, "hi")
tuple.0
```
---
## Dict
```
dict.new()
dict.insert(d, key, value)
dict.delete(d, key)
```
unordered hashmap.
---
# Constants
```gleam
const nums = [1,2,3]
```
* must be **literal**
---
# Functions
```
fn add(a: Int, b: Int) -> Int {
a + b
}
```
private unless `pub`.
---
# Anonymous Functions
```
fn(x) { x + 1 }
```
closures allowed.
---
# Function Types
```
fn(Int) -> Int
```
example:
```
fn twice(x: Int, f: fn(Int) -> Int) {
f(f(x))
}
```
---
# Function Capture
```
add(1, _)
```
equivalent:
```
fn(x) { add(1, x) }
```
---
# Generics
```
fn twice(value: t, f: fn(t) -> t) -> t {
f(f(value))
}
```
type variables: lowercase.
---
# Pipe Operator
```
value
|> f()
|> g()
|> h
```
Equivalent:
```
h(g(f(value)))
```
---
# Labels
```
fn calc(value: Int, add addend: Int) {
}
```
Call:
```
calc(1, add: 2)
```
Shorthand:
```
calc(addend:)
```
---
# Blocks
```
{
let x = 1
x + 1
}
```
last expression returned.
---
# Pattern Matching
```
case value {
0 -> "zero"
_ -> "other"
}
```
Rules:
* exhaustive
* `_` wildcard
---
# Pattern Types
### Variable
```
case x {
y -> ...
}
```
### String prefix
```
"Hello, " <> name
```
### Lists
```
[]
[1]
[first, ..rest]
```
### Multiple subjects
```
case x, y {
0,0 -> ...
}
```
### Alternatives
```
1 | 3 | 5 -> "odd"
```
### Guards
```
[first, ..] if first > 10
```
---
# Recursion
No loops.
```
fn factorial(x) {
case x {
0 -> 1
_ -> x * factorial(x-1)
}
}
```
---
# Tail Recursion Pattern
```
pub fn factorial(x) {
loop(x,1)
}
fn loop(x, acc) {
case x {
0 -> acc
_ -> loop(x-1, acc*x)
}
}
```
---
# Custom Types
```
pub type Season {
Spring
Summer
Autumn
Winter
}
```
Use:
```
case season {
Spring -> ...
}
```
---
# Records
```
pub type Person {
Person(name: String, age: Int)
}
let p = Person("Amy", 26)
p.name
```
Update:
```
Person(..p, age: 27)
```
---
# Generic Types
```
pub type Option(a) {
Some(a)
None
}
```
---
# Result Type
```
Result(value, error)
Ok(value)
Error(reason)
```
Pattern:
```
case result {
Ok(v) -> ...
Error(e) -> ...
}
```
---
# Nil
```
Nil
```
unit type.
---
# Standard Library Highlights
### list
```
list.map(xs, fn(x){})
list.filter(xs, fn(x){})
list.fold(xs, acc, fn(a,x){})
list.find(xs, fn(x){})
```
---
### result
```
result.map(r, fn)
result.try(r, fn)
result.unwrap(r, default)
```
---
### option
```
Some(value)
None
```
---
# Special Keywords
### todo
```
todo
todo as "reason"
```
compile warning + runtime crash.
---
### panic
```
panic as "message"
```
should rarely be used.
---
### assert
```
assert x == y
```
test assertions.
---
# Opaque Types
```
pub opaque type PositiveInt {
PositiveInt(inner: Int)
}
```
constructors private to module.
---
# External Functions
```
@external(erlang, "calendar", "local_time")
@external(javascript, "./ffi.mjs", "now")
pub fn now() -> DateTime
```
---
# Use Expression
```
use x <- result.try(a())
use y <- result.try(b())
x + y
```
expands to nested callbacks.
---
# Bit Arrays
```
<<3>>
<<x:size(16)>>
<<"text":utf8>>
<<a:bits, b:bits>>
```
binary data syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment