[erlang-questions] foldl vs foldr and ++

José Valim jose.valim@REDACTED
Wed May 9 21:14:18 CEST 2012

> My understanding is that foldl is preferred over foldr because it is tail
> recursive. I was also to understand that putting the accumulator on the
> left-hand-side of the ++ operation is super bad.

According to Erlang and OTP in Action, the cost of ++ is specified by the
left side. ++ copies the items on the left side on top of the right side
which explains the performance difference. If the order does not matter,
always put the smaller list on the left side.

José Valim
Founder and Lead Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120509/5d4af62b/attachment.htm>

More information about the erlang-questions mailing list