Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Not in scope: foldl'?

Tags:

module

haskell

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?

like image 547
Tengu Avatar asked May 03 '13 16:05

Tengu


1 Answers

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.

like image 127
C. A. McCann Avatar answered Oct 17 '22 06:10

C. A. McCann