[erlang-questions] Strange behaviour of element/2 inside list comprehensions

Oscar Hellström oscar@REDACTED
Tue Sep 25 11:01:14 CEST 2007


Hi,

I've noticed that element/2 doesn't exit with a badarg as I expected 
when used in a list comprehension. Other functions that exit with badarg 
are behaving as expected though. Is this actually the intended 
behaviour? (I'm using R11B-4 but have also seen it on R11B-3).

Example:
1> [Foo || Foo <- [{1,2}], element(3, Foo) /= 9].
[]
5> [Foo || Foo <- [{1,2}], list_to_integer(Foo) /= 10].

=ERROR REPORT==== 25-Sep-2007::09:58:45 ===
Error in process <0.38.0> with exit value: 
{badarg,[{erlang,list_to_integer,[{1,2}]},{erl_eval,do_apply,5},{erl_eval,expr,5},{erl_eval,eval_filter,6},{erl_eval,eval_generate,7},{erl_eval,eval_lc,6},{shell,exprs,6},{shell,eval_loop,3}]}

...
3> element(3, {1,2}).

=ERROR REPORT==== 25-Sep-2007::09:55:30 ===
Error in process <0.31.0> with exit value: 
{badarg,[{erlang,element,[3,{1,2}]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

...
4> > lists:filter(fun(Foo) -> element(3, Foo) /= 9 end, [{1,2}]).

=ERROR REPORT==== 25-Sep-2007::09:56:45 ===
Error in process <0.36.0> with exit value: 
{badarg,[{erlang,element,[3,{1,2}]},{erl_eval,do_apply,5},{erl_eval,expr,5},{lists,'-filter/2-lc$^0/1-0-',2},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

Best Regards
-- 
Oscar Hellström, oscar@REDACTED
Erlang Training and Consulting
http://www.erlang-consulting.com/



More information about the erlang-questions mailing list