【问题标题】:Importing Prelude function, doctest says 'Not in scope'导入 Prelude 功能,doctest 说“不在范围内”
【发布时间】:2014-09-27 14:07:14
【问题描述】:

尝试使用 Prelude 的内置函数按空格分隔符列出字符串,如 SO answer here 所述。

我有以下几点:

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'

但我在运行 Doctest 时仍然遇到错误:

Not in scope: ‘Char.isSpace’ 

两行。我已经导入了,为什么不在范围内?

【问题讨论】:

  • 尝试删除isSpace前面的Char.。或import qualified Data.Char as Char.
  • 把它当做答案,而且是错误的奖励:)

标签: haskell


【解决方案1】:

线

import Data.Char

isSpace 放入作用域,不带Char. 前缀。因此,删除上述前缀就足够了。

否则,

import qualified Data.Char as Foo

将使用您选择的任何前缀Foo.Foo.isSpace 放入范围(以及导入模块的其余部分)。

【讨论】:

    【解决方案2】:

    您已经得到了很好的建议,但作为进一步的解释,这里可能发生的情况是您尝试调整的代码使用了Data.Char 模块的 Haskell98 名称,它只是Char。 (如果启用 GHC 的 Haskell98 模式,仍然可以通过这种方式导入。)

    在 H98 标准之后一段时间,添加了带有点的分层模块名称,当人们看到完全扁平的模块命名空间不切实际时,这是事后的想法。但这是以最小的方式完成的,只需在模块名称中添加 . 作为允许的字符。

    特别是,模块名称在 Haskell 中是不可可分割的:导入模块名称 Data.Char 本身并不能让您使用 Char 作为模块前缀。

    因此,如果您确实想要在 isSpace 之前添加一个模块前缀,然后只使用 import Data.Char,则必须使用完整的模块名称:Data.Char.isSpace

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多