[erlang-questions] Clueless am I
Richard A. O'Keefe
Tue Apr 26 07:34:12 CEST 2016
On 26/04/16 7:39 AM, Donald Steven wrote:
> Stefan and Hugo,
> Is there any data structure in Erlang that will accept modification?
No. This is a major safety feature of Erlang.
> This is quite beyond me as to how to add elements to a data structure.
You can't, and you don't want to. You create a *NEW* data structure
containing the additional information PLUS the information in the old
data structure, sharing much of the original data structure. There is
by now a substantial literature on "persistent data structures", meaning
ones where you create new versions of data structures and the old
versions can still hang around. This is fantastic for "undo", for example.
> I've hit the same dead end with lists. If one has, for example, a
> database, and one wants to add a customer, do you really need to have
> a new (second) database?
Data *bases* are not your average data *structure*. Amongst other things,
Erlang data *structures* are private to a process, whereas the point of
data *bases* is to be shared between processes. Erlang has mutable data
*bases* (ETS, DETS, Mnesia, an assortment of interfaces to 3rd party
Horses for courses. (And mutations to data bases are indeed nasty to
any programming language.)
Put it this way: if you have a variable X holding the number 5
and you compute X+1, you have "added 1 to X", but 5 has not
been changed into 6. (Unless you have a very old Fortran
compiler where that bug was not unknown.) Data structures in Erlang
act like numbers.
More information about the erlang-questions