Array.of_seq strange behaviour

stdlib
#1

Hi everybody. I had been playing with sequences from the standard library, and the various data structures they can be converted to, when I found the curious following behaviour for Array.of_seq :

# [1;2;3;4] |> List.to_seq |> List.of_seq;;
- : int list = [1; 2; 3; 4]
# [|1;2;3;4|] |> Array.to_seq |> Array.of_seq;;
- : int array = [|4; 1; 2; 3|]
# [|1;2;3;4|] |> Array.to_seq |> List.of_seq;;
- : int list = [1; 2; 3; 4]
# [1;2;3;4] |> List.to_seq |> Array.of_seq;;
- : int array = [|4; 1; 2; 3|]

Is this a bug, or is the rotation in the obtained array normal and to be expected ? In the latter case, may you tell me the reasons behind it ? The OCaml reference for the Array module just states “Create an array from the generator” for the of_seq function. There is no note explaining why this happens.

Thanks a lot for your answers.

— cloudyhug

#2

Looks like a bug in

The function sets the array’s initial element to head of list, but this is actually the last element rather than the first. Then overwrites everything except first element of the array.

#3

Actually as i look at it this code seems fine… it overwrites everything except last element.

#4

This is a bug and has been fixed in 4.07.1

1 Like