[erlang-questions] Clueless am I

Hugo Mills hugo@REDACTED
Mon Apr 25 21:51:39 CEST 2016


On Mon, Apr 25, 2016 at 03:39:39PM -0400, Donald Steven wrote:
> Stefan and Hugo,
> 
> Is there any data structure in Erlang that will accept modification?
> This is quite beyond me as to how to add elements to a data
> structure.  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?

   Yes, you do exactly that. :)

   *However*, most of the main data structures are designed quite
carefully to be efficient about modifications. So, for example, adding
something to the start of a list is a trivial operation. The result is
a new list, but the system doesn't copy the whole of the old list --
lists in Erlang are singly-linked, so adding the new item is just
creating a small data structure to hold the new item and pointing its
"next" pointer to the original list.

   So, typically, modifying a large data structure involves making a
small change to part of it, and leaving the rest of it unchanged. Data
structures are automatically copy-on-write things. You can do this
precisely *because* the data structures are immutable: the system can
reuse the existing unchanged parts of the data structure because they
won't be modified once they're set.

   This is a feature, not a restriction. It's kind of hard to get used
to it at first if you're used to everything being mutable, but it
starts making sense once you've used it for a while.

   You have a variety of different basic data structures to work with:
tuples, lists, maps, dicts. These are all efficiently updatable, so
creating "a new (second) database" is an operation that is going to be
reasonably competitive with modify-in-place both in time and in space
used.

   Hugo.

> Don
> 
> On 04/25/2016 03:29 PM, Stefan Schmiedl wrote:
> >Hi Don,
> >
> >these are not the arrays you're looking for.
> >
> >http://erlang.org/doc/man/array.html starts with "Functional, extendible
> >arrays".
> >A bit further down you can find "The representation is not documented and is
> >subject to change without notice."
> >
> >So the array datatype is not the contiguous block of memory you are used to,
> >but a functional data structure. This means that an array, is basically
> >read-only.
> >
> >Mathematical functions are reproducible: You pass in the same arguments, you
> >get
> >the same result. So the array A1 in your non-working example references the
> >original
> >datastructure and your code is equivalent to
> >
> >>      A1 = array:set(0, nextevent(), array:new(20)),
> >>      A2 = array:set(1, nextevent(), A1),
> >>      A3 = array:set(2, nextevent(), A1),
> >>      A4 = array:set(3, nextevent(), A1),
> >>      A5 = array:set(4, nextevent(), A1),
> >s.
> >
> >>-----Ursprüngliche Nachricht-----
> >>Von: erlang-questions-bounces@REDACTED [mailto:erlang-questions-
> >>bounces@REDACTED] Im Auftrag von Donald Steven
> >>Gesendet: Montag, 25. April 2016 21:16
> >>An: erlang-questions@REDACTED
> >>Betreff: [erlang-questions] Clueless am I
> >>
> >>Dear friends,
> >>
> >>Please forgive me for being clueless.  (I'm sure it's because I come to
> >Erlang
> >>steeped in C.)
> >Yes. SCNR.
> >
> >  Below are 2 very simple programs.  Both run: the first one
> >>works (but requires all these variables which are meaningless for my
> >>purposes) but the second one  does not set the array values and I don't
> >>understand why not or how to do this.
> >>
> >>Thank for any help you can provide.
> >>
> >>Don
> >>
> >>%% THIS WORKS
> >>
> >>-module(arraytest).
> >>-export([main/0]).
> >>
> >>main() ->
> >>
> >>      A1 = array:set(0, nextevent(), array:new(20)),
> >>      A2 = array:set(1, nextevent(), A1),
> >>      A3 = array:set(2, nextevent(), A2),
> >>      A4 = array:set(3, nextevent(), A3),
> >>      A5 = array:set(4, nextevent(), A4),
> >>
> >>      io:format("~nArray: ~p~n", [A5]),
> >>      io:format("Array size: ~p~n~n", [array:size(A5)]).
> >>
> >>nextevent() ->
> >>
> >>      [rand:uniform()].
> >>
> >>============================================
> >>
> >>%% THIS DOES NOT WORK
> >>
> >>-module(arraytest).
> >>-export([main/0]).
> >>
> >>main() ->
> >>
> >>      A1 = array:set(0, nextevent(), array:new(20)),
> >>      array:set(1, nextevent(), A1),
> >>      array:set(2, nextevent(), A1),
> >>      array:set(3, nextevent(), A1),
> >>      array:set(4, nextevent(), A1),
> >>
> >>      io:format("~nArray: ~p~n", [A1]),
> >>      io:format("Array size: ~p~n~n", [array:size(A1)]).
> >>
> >>nextevent() ->
> >>
> >>      [rand:uniform()].
> >>
> >>_______________________________________________
> >>erlang-questions mailing list
> >>erlang-questions@REDACTED
> >>http://erlang.org/mailman/listinfo/erlang-questions
> 

-- 
Hugo Mills             | I think that everything darkling says is actually a
hugo@REDACTED carfax.org.uk | joke. It's just that we haven't worked out most of
http://carfax.org.uk/  | them yet.
PGP: E2AB1DE4          |                                                Vashka
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160425/4582ccea/attachment.bin>


More information about the erlang-questions mailing list