# Array.of\_seq strange behaviour

**URL:** <https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785>\
**Category:** Learning\
**Tags:** stdlib\
**Created:** [May 10, 2019, 6:04pm UTC](https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785 "2019-05-10T18:04:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloudyhug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cloudyhug/32/2399_2.png) [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Post date:** [May 10, 2019, 6:04pm UTC](https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785/1 "2019-05-10T18:04:20Z")

</div>

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` :

```ocaml
# [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

---

<div class="post-metadata">

**Author:** ![ins](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ins/32/923_2.png) [@ins](https://discuss.ocaml.org/u/ins)\
**Post date:** [May 10, 2019, 7:17pm UTC](https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785/2 "2019-05-10T19:17:38Z")

</div>

Looks like a bug in

> <https://github.com/ocaml/ocaml/blob/8b70a5672d62285612ff9049b79db10e3b17933a/stdlib/array.ml#L331>

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.

---

<div class="post-metadata">

**Author:** ![ins](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ins/32/923_2.png) [@ins](https://discuss.ocaml.org/u/ins)\
**Post date:** [May 10, 2019, 7:20pm UTC](https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785/3 "2019-05-10T19:20:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [May 10, 2019, 7:29pm UTC](https://discuss.ocaml.org/t/array-of-seq-strange-behaviour/3785/4 "2019-05-10T19:29:42Z")

</div>

This is a bug and has been [fixed](https://github.com/ocaml/ocaml/blob/8b70a5672d62285612ff9049b79db10e3b17933a/Changes#L1169-L1171) in 4.07.1
