开发者

Erlang exception Error - no function clause matching lists:map - what am I missing?

I am working on Euler 8. After a bit of reading i decided that use of the map function 开发者_StackOverflowwould solve a problem for me. Throwing a simple test program together to make sure I understood the concepts came up short.

From within the 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})

I see the fun, and the list in the message. What concept am I missing here?


your List is actually a tuple. {} is for tuples, [] is for lists.

your example should be:

1> List = [3,1,4].
[3,1,4]
2> lists:map(fun(Z) -> Z*Z end, List).
[9,1,16]


You are trying to apply lists:map function on tuple. Initiate List = [3,1,4] not as List = {3,1,4} and apply the same function, you will get desired output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜