ocaml: phase 6 List.combine/split/iter2/fold_left2/map2 (+4, 367 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Mechanical pair-walk OCaml implementations. failwith on length mismatch matches Stdlib semantics. List module now covers 30+ functions.
This commit is contained in:
@@ -157,6 +157,47 @@
|
||||
end
|
||||
|
||||
let stable_sort = sort
|
||||
|
||||
let rec combine xs ys =
|
||||
match xs with
|
||||
| [] -> (match ys with
|
||||
| [] -> []
|
||||
| _ -> failwith \"List.combine: unequal lengths\")
|
||||
| hx :: tx ->
|
||||
match ys with
|
||||
| [] -> failwith \"List.combine: unequal lengths\"
|
||||
| hy :: ty -> (hx, hy) :: combine tx ty
|
||||
|
||||
let rec split lst =
|
||||
match lst with
|
||||
| [] -> ([], [])
|
||||
| (a, b) :: t ->
|
||||
(match split t with
|
||||
| (xs, ys) -> (a :: xs, b :: ys))
|
||||
|
||||
let rec fold_left2 f acc xs ys =
|
||||
match xs with
|
||||
| [] -> (match ys with [] -> acc | _ -> failwith \"List.fold_left2: unequal\")
|
||||
| hx :: tx ->
|
||||
match ys with
|
||||
| [] -> failwith \"List.fold_left2: unequal\"
|
||||
| hy :: ty -> fold_left2 f (f acc hx hy) tx ty
|
||||
|
||||
let rec iter2 f xs ys =
|
||||
match xs with
|
||||
| [] -> (match ys with [] -> () | _ -> failwith \"List.iter2: unequal\")
|
||||
| hx :: tx ->
|
||||
match ys with
|
||||
| [] -> failwith \"List.iter2: unequal\"
|
||||
| hy :: ty -> f hx hy; iter2 f tx ty
|
||||
|
||||
let rec map2 f xs ys =
|
||||
match xs with
|
||||
| [] -> (match ys with [] -> [] | _ -> failwith \"List.map2: unequal\")
|
||||
| hx :: tx ->
|
||||
match ys with
|
||||
| [] -> failwith \"List.map2: unequal\"
|
||||
| hy :: ty -> f hx hy :: map2 f tx ty
|
||||
end ;;
|
||||
|
||||
module Option = struct
|
||||
|
||||
Reference in New Issue
Block a user