[Haskell-cafe] how to optmize this code?

Felipe Almeida Lessa felipe.lessa at gmail.com
Thu Mar 31 05:59:26 CEST 2011

On Wed, Mar 30, 2011 at 2:39 PM, Gilberto Garcia <giba.dmb at gmail.com> wrote:
> fkSum :: Int -> [Int] -> Int
> fkSum a [] = 0
> fkSum a (b) = foldl (+) 0 (filter (\x -> isMultiple x b) [1..a])

Daniel Fischer and Yves Parès gave you good suggestions about
implementing a different, better algorithm for you problem.  However,
there's one small thing about your current code.  Instead of foldl,
you should use foldl' (use "import Data.List"), which is strict in the
accumulator.  Most of the time you want foldl' instead of foldl.  You
can learn more about the list folds here [1].


[1] http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27


More information about the Haskell-Cafe mailing list