Files
rose-ash/lib/ocaml/baseline/memo_fib.ml
giles fff8fe2dc8
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
ocaml: phase 5.1 memo_fib.ml baseline (16/16 pass)
Memoized fibonacci using Hashtbl.find_opt + Hashtbl.add.
fib(25) = 75025. Demonstrates mutable Hashtbl through the OCaml
stdlib API in real recursive code.
2026-05-08 19:10:49 +00:00

16 lines
288 B
OCaml

(* Baseline: memoized fibonacci using Hashtbl *)
let cache = Hashtbl.create 16 ;;
let rec fib n =
if n < 2 then n
else
match Hashtbl.find_opt cache n with
| Some v -> v
| None ->
let v = fib (n - 1) + fib (n - 2) in
Hashtbl.add cache n v ;
v
;;
fib 25