[erlang-questions] At what point am I "playing compiler"

Tony Rogvall <>
Sun May 17 23:23:13 CEST 2009


Currently the compiler does not know what lists:foldl does, so it  
makes it hard
for the compiler to optimise code like this. There is an efficiency  
guide in the
OTP documentation describing things like this. http://www.erlang.org/doc/
under Erlang Programming/Efficency Giud. I could not make a nice link  
to it!

If list functions like lists:map/lists:fold where to be given strict  
semantics,
just meaning that compiler knows what result should be computed for  
any kind of input.
Then the compiler can use this knowledge to optimise code. With the  
dynamic module
system the lists:map could basically do one thing today and something  
else tomorrow,
unless some one decide that lists:map is defined in a precise way.

We already have some lists functions in the system that (like reverse)  
that has found
it's way into the runtime system. Thereby qualifying both the lists  
module and the
functions within the lists module to be a bit special.

Maybe it's time to integrate some of the high order lists  functions  
as part of the
language ?

/Tony


On 17 maj 2009, at 19.11, Dennis Byrne wrote:

> The functions expressive/0 and efficient/0 have the same result.
> Sometimes I prefer expressive syntax but I am concerned about
> efficiency.  Should Erlang developers worry about this or do any of
> the compilers (or runtime) make these concerns obselete?
>
> expressive() ->
> 	List = [1,2,3],
> 	Last = lists:last(List),
> 	Min = lists:foldl(fun min/2, Last, List),
> 	Max = lists:foldl(fun max/2, Last, List),
> 	Sum = lists:foldl(fun sum/2, 0, List),
> 	{Min, Max, Sum}.
>
> efficient() ->
> 	List = [1,2,3],
> 	Last = lists:last(List),
> 	lists:foldl(fun summary/2, {Last, Last, 0}, List).
> 	
> summary(X, {Min, Max, Total}) ->	
> 	{min(X, Min), max(X, Max), Total + X}.
> 			
> sum(X, Y) ->
> 	X + Y.
>
> min(X, Y) when X < Y ->
> 	X;
> min(_, Y) ->
>  	Y.
>
> max(X, Y) when X > Y ->
> 	X;
> max(_, Y) ->
> 	Y.
>
> -- 
> Dennis Byrne
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list