I have the following code in list.erl:
-module(list).
-export([average/1]).
average(X) when list(X) -> sum(X) / len(X).
sum([H|T]) -> H + sum(T);
sum([]) -> 0.
len([_|T]) -> 1 + len(T);
len([]) -> 0.
Loading this module in the erl shell gives a Warning message.
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1> c(list).
list.erl:4: Warning: list/1 obsolete
{ok,list}
2>
When/Why does this happen? Thanks.
You need to use at guard is_list(X)
.
-module(list).
-export([average/1]).
average(X) when is_list(X) -> sum(X) / len(X).
sum([H|T]) -> H + sum(T);
sum([]) -> 0.
len([_|T]) -> 1 + len(T);
len([]) -> 0.