Scott Encoding
Tutorial Video
Text description (accessibility)
This video demonstrates the "Scott Encoding" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. Scott encoding is an alternative to Church encoding that represents data as pattern matches — a function that takes one continuation per constructor. Key difference from OCaml: 1. **Pattern match access**: Scott encoding gives O(1) `head`/`tail` (pattern match access); Church encoding gives O(1) fold but O(n) pattern match equivalent.
Tutorial
The Problem
Scott encoding is an alternative to Church encoding that represents data as pattern matches — a function that takes one continuation per constructor. Where Church encoding ties data to iteration, Scott encoding ties data to pattern matching. Scott-encoded natural numbers and lists support O(1) head and tail (Church lists need O(n) to access the tail), making Scott encoding more practical for functional data structures. It is the basis for how some functional compilers represent data types at runtime.
🎯 Learning Outcomes
match in functional languages maps to Scott's continuation-passing patternCode Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
head/tail (pattern match access); Church encoding gives O(1) fold but O(n) pattern match equivalent.Box<dyn Fn> types; OCaml infers all types automatically.OCaml Approach
OCaml's variant types are the standard encoding; Scott encoding is the lambda calculus explanation of how they work:
(* Scott natural number: one continuation per constructor *)
let zero zero_c _succ_c = zero_c
let succ n _zero_c succ_c = succ_c n
let predecessor n = n (fun () -> zero) (fun prev -> prev)
In OCaml, this is purely educational — actual code uses type nat = Zero | Succ of nat with pattern matching.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Scott Encoding
Overview
See the example.rs and example.ml files for detailed implementations.
Key Differences
| Aspect | OCaml | Rust |
|---|---|---|
| Type system | Hindley-Milner | Ownership + traits |
| Memory | GC | Zero-cost abstractions |
| Mutability | Explicit ref | mut keyword |
| Error handling | Option/Result | Result<T, E> |
See README.md for detailed comparison.
Exercises
nil, cons, head, and tail operations.predecessor(succ(succ(zero))) produces succ(zero) using scott_to_int.add_scott for Scott naturals — it will be more complex than Church's add because you need to recurse differently.