?? OCaml Sample Code

A curated collection of idiomatic OCaml programs demonstrating core functional programming concepts

OCaml License Stars
??

Hello OCaml

Let bindings, type inference, pipes, pattern matching on tuples

hello.ml
??

Last Element

Option types, safe list traversal, recursive pattern matching

list_last_elem.ml
??

Prime Factorization

Mutual recursion, trial division, input validation

factor.ml
??

Binary Search Tree

Algebraic data types, polymorphism, O(n) traversal with accumulators

bst.ml
??

Merge Sort

Higher-order functions, tail recursion, parameterized comparison

mergesort.ml
🐇

Fibonacci

Hash tables, closures, memoization, benchmarking three approaches

fibonacci.ml
🕸️

Graph Algorithms

BFS, DFS, shortest path, cycle detection, topological sort, connected components

graph.ml
⛰️

Priority Queue

Leftist min-heap, merge-based design, persistence, heap sort, top-k

heap.ml
🧩

Parser Combinators

Monadic composition, closures, recursive descent, operator precedence, arithmetic evaluator

parser.ml
🔤

Trie (Prefix Tree)

Map.Make functor, recursive records, prefix search, auto-complete, persistence, tree pruning

trie.ml
📄

JSON Parser

Full RFC 8259 parser, pretty printer, dot-notation queries, transforms, depth-limited parsing

json.ml
🔍

Regex Engine

Thompson's NFA construction, linear-time matching, character classes, find/replace/split

regex.ml
🔴

Red-Black Tree

Self-balancing BST, Okasaki's balance function, persistent immutable structure

rbtree.ml
🗺️

Dijkstra's Algorithm

Weighted graphs, shortest paths, Floyd-Warshall all-pairs, Prim's MST

dijkstra.ml
🗄️

Hash Map

Persistent hash map, separate chaining, auto-resize, fold/map/filter/merge

hashmap.ml
🧊

LRU Cache

Bounded cache with LRU eviction, shadow indexing, hit/miss statistics

lru_cache.ml
🌸

Bloom Filter

Probabilistic set membership, no false negatives, optimal sizing, union/intersection

bloom_filter.ml
🐦

Cuckoo Filter

Probabilistic membership with deletion support, cuckoo hashing, fingerprints

cuckoo_filter.ml
⏭️

Skip List

Probabilistic sorted structure, express lanes, range queries, O(log n) expected

skip_list.ml
📦

Huffman Coding

Optimal prefix-free compression, tree construction, bit packing, encode/decode

huffman.ml
🔗

Monad Playground

Interactive monad tutorial with bind chain visualizer, law verifier, exercises, and comparison

monads.html
⚙️

Abstract Machines

SECD, CEK, and Krivine machines — three classic evaluation strategies for the lambda calculus

abstract_machine.ml
🖥️

Bytecode VM

Stack-based virtual machine with compiler, disassembler, call frames, and bounded execution

bytecode_vm.ml
🔀

Petri Nets

Concurrency modeling with reachability graphs, deadlock detection, boundedness, and liveness analysis

petri_net.ml
📐

Theorem Prover

Propositional logic prover with sequent-calculus proof search, NNF, truth tables, and entailment

theorem_prover.ml
📼

Turing Machine

Tape-based simulator with binary increment, palindrome checker, busy beaver, and verbose tracing

turing_machine.ml
🎨

Ray Tracer

Recursive ray tracer with Phong shading, reflections, shadows, and multi-sample anti-aliasing

raytracer.ml

Quick 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.