导入前奏功能,doctest说'不在范围内' [英] Importing Prelude function, doctest says 'Not in scope'

查看:99
本文介绍了导入前奏功能,doctest说'不在范围内'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Prelude的内置函数来通过空格分隔符来判断字符串,正如SO回答此处所述。



我有以下内容:

  module MiniForth 
(函数
,...
)其中
导入Data.Char - 我在这里实际导入
导入前缀隐藏(单词) - 这避免了单词function当在本地声明时

words :: String - > [字符串]
- ^取出一个字符串并将其分成单独的由空格分隔的单词
-
- 示例:
-
- > ;>词在空格处打破此字符串
- [break,this,string,at,spaces]
-
- >> ;单词
- []
-
单词s =大写字母dropWhile
- >& []
s' - > w:words s''
where(w,s'')= break Char.isSpace s'

但运行Doctest时仍然出现错误:

 不在范围内:'Char.isSpace'$ b $对于两条线,b  

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

解决方案

  import Data.Char 

puts isSpace 放入作用域,而不需要 Char。前缀。因此,删除所述前缀就足够了。



否则,

 输入合格的Data.Char为Foo 

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


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?

解决方案

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.

这篇关于导入前奏功能,doctest说'不在范围内'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆