[Haskell-beginners] Is foldr slow?

Zhi-Qiang Lei zhiqiang.lei at gmail.com
Sun Feb 5 11:33:35 CET 2012

Thank you very much. You are right. foldl performs much better here. I thought foldr always has a performance advantage due to its tail recursion.

On Feb 3, 2012, at 5:36 AM, Chaddaï Fouché wrote:

> let go a b = b `minus` (multiplies a c)
> foldr go cs' (p:p':ps) ==> foldr go cs' (p':ps) `minus` multiplies p c
> ==> (minus needs his first argument to start removing stuff) (foldr go
> cs' ps `minus` multiplies p' c) `minus` multiplies p c
> And so on and so forth...
> In other words, contrary to your first version, this function must
> develop the entire list before making its first suppression...
> Your version rather correspond to a foldl (be careful of strictness,
> using foldl it's pretty easy to get big thunks).
> Foldr is very useful for functions that are lazy in their second
> argument like (||) , (&&),  (:) or others but if the function is
> strict in its second argument like yours (strict in b)...
> -- 
> Jedaï

Best regards,
Zhi-Qiang Lei
zhiqiang.lei at gmail.com

More information about the Beginners mailing list