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