Inplace array update

38 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Kuba Roth

nieprzeczytany,
31 maj 2016, 05:24:4131.05.2016
do ocaml-core
Could anybody help me out understanding why below example does not work on the array please?

let a = [|1;2;3|];;
Array.iter (fun x -> x <- 2) a;;
Error: The value x is not an instance variable

Thanks,
kuba

Jon Harrop

nieprzeczytany,
31 maj 2016, 05:28:2931.05.2016
do ocaml...@googlegroups.com

The function you passed to Array.iter is given the value of the element in the array rather than a reference to it. Consequently, it is immutable.

--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David House

nieprzeczytany,
31 maj 2016, 05:32:4031.05.2016
do ocaml...@googlegroups.com
To modify elements of an array you want something like:

a.(0) <- 2

juuso....@gmail.com

nieprzeczytany,
4 cze 2016, 21:37:264.06.2016
do ocaml-core
Array.iteri will provide you with the current index, which you can use to update the array using the syntax David mentioned
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0