[erlang-questions] Clueless am I

Hugo Mills hugo@REDACTED
Mon Apr 25 21:29:50 CEST 2016


On Mon, Apr 25, 2016 at 03:15:56PM -0400, Donald Steven wrote:
> Dear friends,
> 
> Please forgive me for being clueless.  (I'm sure it's because I come
> to Erlang steeped in C.)  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.

   The thing to remember is that in general Erlang's variables are
immutable -- so once you've set the value of a variable, it can't be
changed. So functions that modify things don't do so in-place, but
instead return a new thing which has the requested modification.

   So what you're doing in the second example is creating a new array,
then creating a new structure which is a modification of that array
(with element 0 set), and setting A1 to the result. Then you're making
a new thing based on A1 (with element 1 set) and throwing it away.
Then you're making a new thing based on A1 (with element 2 set) and
throwing it away... At the end of all this, you're using the original
A1, which only has element 0 set.

   The first example works because you're *not* throwing away the
modified copy each time, but instead setting it to a new variable, and
using that for the next call. So yes, the variables *are* useful, and
no, they *aren't* meaningless to you -- they're the sequence of
modifications you've made to the array.

   Hugo.

> 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/e59b06d9/attachment.bin>


More information about the erlang-questions mailing list