我是 Erlang 的新手,我正在尝试编写一些函数,该函数将针对给定的 N -> 2, 4, .... 2^N
返回以下几何级数:一个列表。
我写了一些代码:
-module(mymod).
-export([create_list/1]).
create_list(1) -> [2];
create_list(N) when N>1 ->
lists:flatten([create_list(N-1)]++[trunc(math:pow(2, N))]).
在 29 之前它都可以正常工作,但是在我超过 30 或更多之后它会返回相同的结果:
[2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,
65536,131072,262144,524288,1048576,2097152,4194304,8388608,
16777216,33554432,67108864,134217728,268435456,536870912|...]
也就是说,只有前 29 项。怎么做才能完整显示?
最佳答案
您可以使用 rp(create_list(30)) 强制 shell 打印整个结果。
关于Erlang:列表扁平化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765080/