fix: add tail recursion

This commit is contained in:
NunoSempere 2023-10-15 01:14:41 +01:00
parent 7ce4658d30
commit 25a27f7fc4
5 changed files with 11 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -34,7 +34,16 @@ let findIndex xs test =
in in
recursiveHelper xs 0 recursiveHelper xs 0
let rec unwind xs = let unwind xs =
let rec tailRecursiveHelper ys acc =
match ys with
| [] -> Ok(acc)
| Error e :: _ -> Error e
| Ok(y) :: ys -> tailRecursiveHelper ys (y :: acc)
in
tailRecursiveHelper xs []
(* previous version, which wasn't tail-recursive
match xs with match xs with
| [] -> Ok([]) | [] -> Ok([])
| Error e:: ys -> Error e | Error e:: ys -> Error e
@ -43,6 +52,7 @@ let rec unwind xs =
| Ok(zs) -> Ok(y :: zs) | Ok(zs) -> Ok(y :: zs)
| Error e -> Error e | Error e -> Error e
) )
*)
(* Basic samplers *) (* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0 let sampleZeroToOne () : float = Random.float 1.0