Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Read-only arrays ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-04-11 (05:31)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Read-only arrays ?
From: David Teller <>

> In my case, I'm writing a small board game my students will need to
> complete in an assignment. So, I can trust that, if something can go
> wrong, they'll find a way to make it happen.
> Essentially, the board is a matrix, abstracted in a module.
> Among other things, module Board provides 
> * a type Board.square of squares
> * a type Board.t of boards (internally defined as a Board.square array
> array)
> * a conversion function Board.to_array : Board.t -> Board.square array
> array (a copy function for the matrix)
> * a conversion function Board.from_array : Board.square array array ->
> Board.t (a copy function for the matrix)
> While performance is hardly critical, it's intellectually unsatisfying
> to have and copy the whole board each time the borders of the module are
> crossed, just to ensure that my students won't do anything wrong enough
> to break the module.

If your board is already abstract, why convert it to arrays?
You could provide the right access functions.
Or is it because you want to be able to update it destructively: then
you need mutable arrays.
If you don't care at all about performance, using lists of lists is
yet another option.

This is actually the strongest argument against immutable arrays: they
are just an abstraction over arrays, that you can easily make yourself
when needed. This is more cumbersome with strings, as we use literals

Jacques Garrigue