?? OCaml Sample Code
A curated collection of idiomatic OCaml programs demonstrating core functional programming concepts
Hello OCaml
Let bindings, type inference, pipes, pattern matching on tuples
hello.mlLast Element
Option types, safe list traversal, recursive pattern matching
list_last_elem.mlPrime Factorization
Mutual recursion, trial division, input validation
factor.mlBinary Search Tree
Algebraic data types, polymorphism, O(n) traversal with accumulators
bst.mlMerge Sort
Higher-order functions, tail recursion, parameterized comparison
mergesort.mlFibonacci
Hash tables, closures, memoization, benchmarking three approaches
fibonacci.mlGraph Algorithms
BFS, DFS, shortest path, cycle detection, topological sort, connected components
graph.mlPriority Queue
Leftist min-heap, merge-based design, persistence, heap sort, top-k
heap.mlParser Combinators
Monadic composition, closures, recursive descent, operator precedence, arithmetic evaluator
parser.mlTrie (Prefix Tree)
Map.Make functor, recursive records, prefix search, auto-complete, persistence, tree pruning
trie.mlJSON Parser
Full RFC 8259 parser, pretty printer, dot-notation queries, transforms, depth-limited parsing
json.mlRegex Engine
Thompson's NFA construction, linear-time matching, character classes, find/replace/split
regex.mlRed-Black Tree
Self-balancing BST, Okasaki's balance function, persistent immutable structure
rbtree.mlDijkstra's Algorithm
Weighted graphs, shortest paths, Floyd-Warshall all-pairs, Prim's MST
dijkstra.mlHash Map
Persistent hash map, separate chaining, auto-resize, fold/map/filter/merge
hashmap.mlLRU Cache
Bounded cache with LRU eviction, shadow indexing, hit/miss statistics
lru_cache.mlBloom Filter
Probabilistic set membership, no false negatives, optimal sizing, union/intersection
bloom_filter.mlCuckoo Filter
Probabilistic membership with deletion support, cuckoo hashing, fingerprints
cuckoo_filter.mlSkip List
Probabilistic sorted structure, express lanes, range queries, O(log n) expected
skip_list.mlHuffman Coding
Optimal prefix-free compression, tree construction, bit packing, encode/decode
huffman.mlMonad Playground
Interactive monad tutorial with bind chain visualizer, law verifier, exercises, and comparison
monads.htmlAbstract Machines
SECD, CEK, and Krivine machines — three classic evaluation strategies for the lambda calculus
abstract_machine.mlBytecode VM
Stack-based virtual machine with compiler, disassembler, call frames, and bounded execution
bytecode_vm.mlPetri Nets
Concurrency modeling with reachability graphs, deadlock detection, boundedness, and liveness analysis
petri_net.mlTheorem Prover
Propositional logic prover with sequent-calculus proof search, NNF, truth tables, and entailment
theorem_prover.mlTuring Machine
Tape-based simulator with binary increment, palindrome checker, busy beaver, and verbose tracing
turing_machine.mlRay Tracer
Recursive ray tracer with Phong shading, reflections, shadows, and multi-sample anti-aliasing
raytracer.mlQuick Start
# Clone and build
git clone https://github.com/sauravbhattacharya001/Ocaml-sample-code.git
cd Ocaml-sample-code
make run
# Or use Docker
docker build -t ocaml-samples .
docker run --rm ocaml-samples
See the Installation Guide for detailed setup instructions.