[erlang-questions] Dict size in guards

James Churchman <>
Sat Feb 12 14:12:33 CET 2011


i seee, so due to improper lists they have different behaviour

Trying :  Len = length([1 , 2 | ok ]).
Gives :	exception error: bad argument in function  length/1 called as length([1,2|ok])

which is guess is ok because the list is improper and length is therefore undefined

and with a guard : 

LengthFun = fun
	(List) when length(List) > 1 -> big;
	(_) -> small
end.

LengthFun( [1 , 2 | ok ] )

Gives small as the guard silently fails.. 

I guess the optimisation could still be made internally in beam somehow but its definitely more complex that it seems at first.



On 12 Feb 2011, at 07:28, Björn Gustavsson wrote:

> That basic optimization is not done because the
> length(L) will cause an exception if the L is improper,
> but only checking 10 elements will not cause an exception.
> The compiler does not do optimizations that would
> silence potential exceptions (silencing exceptions could
> hide bugs in the program and make debugging more
> difficult).
> 
> Here is an example of an improper list that will
> cause a badarg exception in length/1:
> 
> lists:seq(1, 20) ++ [a|b]



More information about the erlang-questions mailing list