ocaml: phase 6 expanded stdlib (+15 tests, 319 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s

List: concat/flatten, init, find/find_opt, partition, mapi/iteri,
assoc/assoc_opt. Option: iter/fold/to_list. Result: get_ok/get_error/
map_error/to_option.

Fixed skip-to-boundary! in parser to track let..in / begin..end /
struct..end / for/while..done nesting via a depth counter — without
this, nested-let inside a top-level decl body trips over the
decl-boundary detector. Stdlib functions like List.init / mapi / iteri
use begin..end to make their nested-let intent explicit.
This commit is contained in:
2026-05-08 12:49:23 +00:00
parent bc557a5ad2
commit 88c02c7c73
4 changed files with 202 additions and 20 deletions

View File

@@ -82,6 +82,67 @@
match lst with
| [] -> failwith \"List.nth: out of range\"
| h :: t -> if n = 0 then h else nth t (n - 1)
let rec concat lst =
match lst with
| [] -> []
| h :: t -> append h (concat t)
let flatten = concat
let rec init n f =
if n = 0 then [] else
begin
let rec build i =
if i = n then [] else f i :: build (i + 1)
in build 0
end
let rec find_opt p lst =
match lst with
| [] -> None
| h :: t -> if p h then Some h else find_opt p t
let rec find p lst =
match find_opt p lst with
| None -> failwith \"List.find: not found\"
| Some x -> x
let rec partition p lst =
match lst with
| [] -> ([], [])
| h :: t ->
(match partition p t with
| (yes, no) ->
if p h then (h :: yes, no) else (yes, h :: no))
let rec mapi f lst =
begin
let rec go i xs =
match xs with
| [] -> []
| h :: t -> f i h :: go (i + 1) t
in go 0 lst
end
let rec iteri f lst =
begin
let rec go i xs =
match xs with
| [] -> ()
| h :: t -> f i h; go (i + 1) t
in go 0 lst
end
let rec assoc k lst =
match lst with
| [] -> failwith \"List.assoc: not found\"
| (k2, v) :: t -> if k = k2 then v else assoc k t
let rec assoc_opt k lst =
match lst with
| [] -> None
| (k2, v) :: t -> if k = k2 then Some v else assoc_opt k t
end ;;
module Option = struct
@@ -114,6 +175,21 @@
match o with
| None -> false
| Some _ -> true
let iter f o =
match o with
| None -> ()
| Some x -> f x
let fold none_v f o =
match o with
| None -> none_v
| Some x -> f x
let to_list o =
match o with
| None -> []
| Some x -> [x]
end ;;
module Result = struct
@@ -136,6 +212,26 @@
match r with
| Ok _ -> false
| Error _ -> true
let get_ok r =
match r with
| Ok x -> x
| Error _ -> failwith \"Result.get_ok: Error\"
let get_error r =
match r with
| Ok _ -> failwith \"Result.get_error: Ok\"
| Error e -> e
let map_error f r =
match r with
| Ok x -> Ok x
| Error e -> Error (f e)
let to_option r =
match r with
| Ok x -> Some x
| Error _ -> None
end ;;
module String = struct