Scheme library for Zig
Find a file
wmedrano b4969136da
Some checks failed
Test / Build and Test (push) Has been cancelled
Release / Build and Release Linux (push) Has been cancelled
Release / Build and Release macOS (push) Has been cancelled
Release / Build and Release Windows (push) Has been cancelled
Compile for more machines
2025-11-13 19:31:38 -08:00
.github/workflows Compile for more machines 2025-11-13 19:31:38 -08:00
examples Fix stdin reading 2025-11-12 23:07:42 -08:00
scripts Implement dynamic parameters (#8) 2025-10-22 19:25:24 -07:00
src Fix stdin reading 2025-11-12 23:07:42 -08:00
.gitignore Change calling convention 2025-10-09 21:36:13 -07:00
build.zig Add diagnostics for read errors (#1) 2025-10-12 23:03:49 -07:00
build.zig.zon Initial commit 2025-10-02 19:28:30 -07:00
CLAUDE.md Catch errors from native functions (#11) 2025-10-30 16:54:23 -07:00
README.md Support rational numbers (#7) 2025-10-19 13:09:59 -07:00

szl

A Scheme interpreter implemented in Zig.

Quickstart

REPL Basics

Sizzle requires Zig 0.15. To start the interactive REPL:

zig build -Doptimize=ReleaseFast
zig-out/bin/szl

Type expressions and press Enter to evaluate them. Use (exit) or Ctrl+D to quit.

Syntax: S-Expressions

Sizzle uses prefix notation with parentheses. Instead of f(x, y), write (f x y).

; 2 + 3 * 4 becomes:
(+ 2 (* 3 4))  ; => 14

; Chained operations
(+ 1 2 3 4 5)  ; => 15
(* 2 3 4)      ; => 24

Variables and Functions

Use define to create variables and functions:

; Variable
(define x 42)

; Function
(define (square n)
  (* n n))

(square 5)  ; => 25

; Anonymous functions with lambda
(define double (lambda (n) (* n 2)))
(double 7)  ; => 14

Local Bindings

Use let to create temporary variables:

(let ((x 5)
      (y 10))
  (+ x y))  ; => 15

In let, bindings happen simultaneously, so they can't reference each other. Use let* for sequential bindings:

; let* allows each binding to use previous ones
(let* ((x 5)
       (y (* x 2)))  ; y can reference x
  (+ x y))  ; => 15

Data Types

; Numbers
42          ; integer
3.14        ; float
1/2         ; rational
(+ 1 2.5)   ; => 3.5
(+ 1/2 1/4) ; => 3/4
(+ 1/2 1/2) ; => 1

; Booleans
#t          ; true
#f          ; false

; Strings
"hello"
(string-append "hello" " " "world")  ; => "hello world"

; Lists
'(1 2 3)                    ; literal list
(list 1 2 3)                ; constructed list
(cons 1 '(2 3))             ; prepend: => (1 2 3)
(car '(1 2 3))              ; first element: => 1
(cdr '(1 2 3))              ; rest: => (2 3)

; Vectors (indexed arrays)
#(1 2 3)                    ; literal vector
(vector 1 2 3)              ; constructed
(vector-ref #(10 20 30) 1)  ; => 20

Control Flow

; If expression
(if (> 5 3)
    "yes"
    "no")  ; => "yes"

; Cond for multiple branches
(define (grade score)
  (cond
    ((>= score 90) "A")
    ((>= score 80) "B")
    ((>= score 70) "C")
    (else "F")))

(grade 85)  ; => "B"

; And/or (short-circuit evaluation)
(and #t #f)      ; => #f
(or #f #t)       ; => #t

Working with Lists

; Map
(map
 (lambda (x) (* x 2))
 '(1 2 3))  ; => (2 4 6)

; Filter
(filter
 (lambda (x) (> x 5))
 '(3 7 2 9))  ; => (7 9)

; Fold/reduce
(fold-left
 + 0
 '(1 2 3 4))  ; => 10

; Length
(length '(1 2 3))  ; => 3

; Check empty
(null? '())   ; => #t
(null? '(1))  ; => #f

Try It Out

Here's a complete example to try in the REPL:

; Define a function to find the maximum in a list
(define (max-list lst)
  (if (null? (cdr lst))
      (car lst)
      (let ((rest-max (max-list (cdr lst))))
        (if (> (car lst) rest-max)
            (car lst)
            rest-max))))

(max-list '(3 7 2 9 1))  ; => 9

; Define a function using higher-order functions
(define (compose f g)
  (lambda (x) (f (g x))))

(define add1 (lambda (x) (+ x 1)))
(define double (lambda (x) (* x 2)))
(define add1-then-double (compose double add1))

(add1-then-double 5)  ; => 12

Building

Build the project using Zig's build system. Sizzle requires Zig 0.15.

zig build -Doptimize=ReleaseFast
zig-out/bin/szl

Testing

Run the test suite:

zig build test --summary all

Documentation

  • Generate API documentation under zig-out/docs: zig build doc
  • Build the website under zig-out/site: scripts/build_website.sh