Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List.init violates evaluation order of standard library #175

Open
mmottl opened this issue Jan 30, 2025 · 0 comments
Open

List.init violates evaluation order of standard library #175

mmottl opened this issue Jan 30, 2025 · 0 comments
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@mmottl
Copy link

mmottl commented Jan 30, 2025

The following code demonstrates inconsistent behavior between the List.init function in Base and the same function in the OCaml standard library due to an incorrect evaluation order of list elements:

open Printf
open Base

let () =
  printf "Stdlib:\n\n";
  let r = ref 0 in
  Stdlib.List.init 10 (fun _i -> Int.incr r; !r) |>
  List.iter ~f:(printf "%d\n")

let () =
  printf "\n\nJane Street:\n\n";
  let r = ref 0 in
  List.init 10 ~f:(fun _i -> Int.incr r; !r) |>
  List.iter ~f:(printf "%d\n")

Output:

Stdlib:

1
2
3
4
5
6
7
8
9
10


Jane Street:

10
9
8
7
6
5
4
3
2
1

Note that the standard library uses [@tail_mod_cons] to achieve tail recursion without having to reverse the list.

@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

2 participants