Adding to an existing value in Erlang

前端 未结 2 991
不知归路
不知归路 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条回答
  •  梦毁少年i
    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
    

提交回复
热议问题