Adding to an existing value in Erlang

末鹿安然 提交于 2019-12-31 04:07:23

问题


I am attempting to create a function that stores a number into a record and then adds value X to that number every time the function runs.

Value: 5
Run Function (Add One):   1
Value should be: 6
Run Function (Add One):   1
value should be 7

I tried to use a record:

-record(adder,{value :: integer()}).

---function 
   Number = random:uniform(6),
        L=#added{value = Number + #added.value}.

This does not work as it resets the value every time. Any suggestions?


回答1:


Take a look at this code:

-module(test).
-export([add/1]).

-record(adder, {value=6}).

add(X) ->
    #adder{value = X + #adder.value}.

If you compile this in your shell, any call to "add(3)" will result in "{adder,5}" and not in "{adder, 9}". Take a look:

Eshell V6.4  (abort with ^G)
1> c(test).
{ok,test}
2> test:add(3).
{adder,5}
3> test:add(3).
{adder,5}

How come? This is because records are actually tuples. The expression "#adder.value" in the last line is evaluated as the position of the field "value" in your adder tuple, which is 2. Let's have some proof. Change the definition of your record:

-module(test).
-export([add/1]).

-record(adder, {field1, field2, value=6}).

add(X) ->
    #adder{value = X + #adder.value}.

Now, recompiling your code and calling add(3) again would result in

1> c(test).
{ok,test}
2> test:add(3).
{adder,undefined,undefined,7}

I've asked myself, how you came up with this question. Didn't you want your function to be something like this?

add2(#adder{value = V} = R, X) ->
    R#adder{value = V + X}.



回答2:


-record(adder, {value = 5}).

add(Value) ->
    add(#adder{}, Value).

add(#adder{value =V} = Adder, Value) ->
    Adder#adder{value = V + Value}.

test() ->
  R1 = add(1),
  io:format("~p~n", [R1]),
  R2 = add(R1, 10),
  io:format("~p~n", [R2]).

Here is the output of running test:test().

6> c(test).
{ok,test}
7> test:test().
{adder,6}
{adder,16}
ok


来源:https://stackoverflow.com/questions/30338353/adding-to-an-existing-value-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!