chris Frisina
chris Frisina

Reputation: 19688

Importing Prelude function, doctest says 'Not in scope'

Trying to use Prelude's built in function to listify a string by space delimiter, as described by SO answer here.

I have the following:

module MiniForth
  ( functions
  , ...
  ) where
import Data.Char  -- I actually import here
import Prelude hiding (words) -- this avoids the ambiguity in the words function when declaring it locally

words :: String -> [String]
-- ^ Takes a string and breaks it into separate words delimited by a space
--
--   Examples:
--
--   >> words "break this string at spaces"
--   ["break","this","string","at","spaces"]
--
--   >> words ""
--   []
--
words s =  case dropWhile Char.isSpace s of
                      "" -> []
                      s' -> w : words s''
                            where (w, s'') = break Char.isSpace s'

but I still get the error when running Doctest:

Not in scope: ‘Char.isSpace’ 

for both lines. I have imported it, so why is it not in scope?

Upvotes: 1

Views: 713

Answers (2)

Ørjan Johansen
Ørjan Johansen

Reputation: 18199

You've already got good suggestions, but as a further explanation, what probably happened here is that the code you were trying to adapt used the old Haskell98 name of the Data.Char module, which was simply Char. (You can still import it that way if you enable GHC's Haskell98 mode.)

The hierarchical module names with dots in them were added a while after the H98 standard, as an afterthought when people saw that a completely flat module namespace was impractical. But this was done in a minimal way, simply adding . as an allowed character in module names.

In particular, module names are not divisible in Haskell: Importing the module name Data.Char does not in itself give you any access to using Char as a module prefix.

So if you did want to include a module prefix before isSpaceafter doing just import Data.Char, it would have to be with the full module name: Data.Char.isSpace.

Upvotes: 2

chi
chi

Reputation: 116174

The line

import Data.Char

puts isSpace into scope, without the Char. prefix. Hence, it is sufficient to remove said prefix.

Otherwise,

import qualified Data.Char as Foo

will put Foo.isSpace into scope (as well as the rest of the imported module), using any prefix Foo. of your choice.

Upvotes: 3

Related Questions