Foldl v foldl' (foldl' seemingly missing?)

Hi :wave:

I’m getting into Haskell will the help fo exercism at the moment, and on one of the exercises had some automated feedback that the lazy foldl tends to leak memory and in most cases the eager foldl' should be preferred.

Only thing is, when I try to use fold' I get an error saying it doesn’t exist:

/mnt/exercism-iteration/src/ReverseString.hs:4:17: error:
    • Variable not in scope:
        foldl' :: ([a0] -> a0 -> [a0]) -> [a1] -> String -> String
    • Perhaps you meant one of these:
        ‘foldl’ (imported from Prelude), ‘foldl1’ (imported from Prelude),
        ‘foldr’ (imported from Prelude)

Have I input it wrong, or does it need to be imported from somewhere? (I’m really new to Haskell, so appologies if the answer is obvious!)

Hello @houseofleft :wave: ,

You have to import it as, for some reason (probably historical reasons), it is not in the Prelude (the “bundle” of functions you readily have available and is loaded automatically).

So put the following in your code:

module ... where
import Data.List (foldl')

(the dots stand for your module’s name, probably ReverseString)

This will import just fold' in your namespace. If you want to import all the other functions from Data.List, put just import Data.List.

1 Like

Ah, I hadn’t realised that! Thanks so much!