As my first module experience, I imported Data.List to my GHCi. (More precisely, I typed import Data.List
on my GHCi) It seems to be working fine because I can use some functions that I did not have before such as foldl'
on my GHCi.
I wrote haha = foldl' (+) 0 [1..10]
on my notepad++, and saved it and loaded then GHCi says Not in scope: foldl'
even though it workds just fine when I type foldl' (+) 0 [1..10]
directly on my GHCi.
Why is that and how can I define functions with foldl'
on my notepad?
What's in scope at the GHCi prompt is not necessarily the same as what's in scope in whatever file you may be loading from GHCi. GHCi has its own notion of current scope, which usually includes the toplevel of whatever file you've loaded plus any other modules you explicitly add or anything you import. (It also behaves differently if loading a file that hasn't been changed since it was last compiled, which still confuses me...)
Anyway, you just need to import Data.List
in the code file itself, e.g.:
module Main where
import Data.List
haha = foldl' (+) 0 [1..10]
After doing that, loading the file should result in Data.List
being effectively imported at the GHCi prompt as well, since it's visible at the toplevel of the loaded module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With