?? 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
⏭️

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

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.