Adding to an existing value in Erlang

前端 未结 2 987
不知归路
不知归路 2021-01-24 09:53

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          


        
相关标签:
2条回答
  • 2021-01-24 10:24

    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}.
    
    0 讨论(0)
  • 2021-01-24 10:32
    -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
    
    0 讨论(0)
提交回复
热议问题