我正在研究Euler 8.经过一番阅读后,我决定使用map函数为我解决问题.将一个简单的测试程序放在一起,以确保我理解这些概念.
从shell内部.
1> List = {3, 1, 4}.
{3,1,4}
2> io:format("oh my ~w ~n", [List]).
oh my {3,1,4}
ok
3> lists:map(fun (Z) -> Z * Z end , List).
** exception error: no function clause matching
lists:map(#Fun<erl_eval.6.80247286>,{3,1,4})
Run Code Online (Sandbox Code Playgroud)
我看到了乐趣和消息中的列表.我在这里错过了什么概念?
小智 11
你的列表实际上是一个元组.{}用于元组,[]用于列表.
你的例子应该是:
1> List = [3,1,4].
[3,1,4]
2> lists:map(fun(Z) -> Z*Z end, List).
[9,1,16]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3515 次 |
| 最近记录: |