erlang - 如何在 Erlang 中使用变量作为引用传递?

标签 erlang pass-by-reference erlang-otp erlang-shell erlang-nif

为什么我的输出没有反射(reflect)在 Lst1 中?

-module(pmap). 
-export([start/0,test/2]). 

test(Lst1,0) ->
   {ok, [Temp]} = io:fread( "Input the edge weight  ", "~d" ),
   lists:append([Lst1,[Temp]]),
   io:fwrite("~w~n",[Lst1]);

test(Lst1,V) ->
   {ok, [Temp]} = io:fread( "Input the edge weight  ", "~d" ),
   lists:append([Lst1,[Temp]]),
   test(Lst1, V-1).

start() -> 
   {ok, [V]} = io:fread( "Input the number of vertices your graph has  ", "~d" ),
   Lst1 = [],
   test(Lst1,V).

所以,我的 Lst1 正在打印 [],而我希望它打印,假设,如果我提供输入 1,2,3,则 [1,2,3]。

最佳答案

因为 Erlang 变量是不可变的,根本无法更改。 lists:append 返回一个您丢弃的新列表。

关于erlang - 如何在 Erlang 中使用变量作为引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64729321/

相关文章:

erlang - 进程字典适合这种情况吗?

c++ - 引用函数指针

erlang - 我应该将静态文本文件放在哪里才能成为 OTPy?

erlang - 是net_kernel :connect/1 deprecated?

erlang - 在 Erlang 中使用完全限定的函数调用?

c++ - 为什么 Pass By Ref 不起作用

Javascript 对引用定义的变量与值的混淆

erlang - simple_one_for_one只有在指定SHUTDOWN策略为brutal_kill时才可以终止?

Erlang容错应用程序: PA or CA of CAP?

windows - RabbitMq 和 "Fatal error: handshake failure - handshake_decode_error"