Inplace array update

38 views
Skip to first unread message

Kuba Roth

unread,
May 31, 2016, 5:24:41 AM5/31/16
to 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

unread,
May 31, 2016, 5:28:29 AM5/31/16
to 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

unread,
May 31, 2016, 5:32:40 AM5/31/16
to ocaml...@googlegroups.com
To modify elements of an array you want something like:

a.(0) <- 2

juuso....@gmail.com

unread,
Jun 4, 2016, 9:37:26 PM6/4/16
to ocaml-core
Array.iteri will provide you with the current index, which you can use to update the array using the syntax David mentioned
Reply all
Reply to author
Forward
0 new messages