Files
rose-ash/lib/ocaml/baseline/lru_cache.ml
giles 800dca67ca
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
ocaml: parser accepts top-level tuple patterns in match cases
Real OCaml accepts `match e1, e2 with | p1, p2 -> …` without
surrounding parens. parse-pattern previously stopped at the cons
layer (`p :: rest`) and treated a trailing `,` as a separator
the outer caller couldn't handle, surfacing as
"expected op -> got op ,".

Fix: `parse-pattern` now collects comma-separated patterns into a
:ptuple after parse-pattern-cons, before the optional `as` alias.
The scrutinee side already built tuples via parse-tuple, so both
sides are now symmetric.

lru_cache.ml (iter 258) reverts its workaround back to the natural
form:

  let rec take n lst = match n, lst with
    | 0, _ -> []
    | _, [] -> []
    | _, h :: r -> h :: take (n - 1) r

607/607 regressions clean.
2026-05-11 00:31:08 +00:00

51 lines
976 B
OCaml

let cache = ref []
let cap = 3
let get k =
let rec find = function
| [] -> None
| (k', v) :: _ when k' = k -> Some v
| _ :: rest -> find rest
in
match find !cache with
| None -> -1
| Some v ->
let rec remove = function
| [] -> []
| (k', _) :: rest when k' = k -> rest
| h :: rest -> h :: remove rest
in
cache := (k, v) :: remove !cache;
v
let put k v =
let rec remove = function
| [] -> []
| (k', _) :: rest when k' = k -> rest
| h :: rest -> h :: remove rest
in
let cleaned = remove !cache in
let trimmed =
if List.length cleaned >= cap then
let rec take n lst = match n, lst with
| 0, _ -> []
| _, [] -> []
| _, h :: r -> h :: take (n - 1) r
in
take (cap - 1) cleaned
else cleaned
in
cache := (k, v) :: trimmed
;;
put 1 100;
put 2 200;
put 3 300;
let a = get 1 in
put 4 400;
let b = get 2 in
let c = get 3 in
let d = get 1 in
a + b + c + d