-4

我被告知要编写一个函数来检查字符串中的标点符号是否都是空格。IE:

haskell> f "Hello my name is Brad"
True

haskell> f "Hello my name is Brad!"
False

我写了一个辅助函数如下,

import Data.Char
isPunc x = not (isDigit x || isAlpha x)

这被 Haskell 接受并且工作正常。但是一旦我在以下功能中使用它,

--function f defined
f :: String -> Bool
f xs = and [ x == ' ' | x <- xs, isPunc x]

它给了我这个错误:

ambiguous occurence 'isPunc', could mean "Main.isPunc" or "Data.Char. isPunc"

我得到了它抱怨的部分内容,但是导入了 Data.Char,我真的不明白它为什么抱怨。

4

2 回答 2

4

(这篇文章是在假设你真正命名你的函数的情况下写的isPunctuation,而不是isPunc

这是模棱两可的isPunctuation,因为 Haskell 不知道您在调用. 它是模棱两可的,因为您导入了 - 如果您没有导入它或者您导入它是合格的,那么就没有歧义,只能指.Main.isPunctuationData.CharisPunctuationisPunctuationData.CharisPunctuationMain.isPunctuation

要解决歧义,要么不导入isPunctuationData.Char通过将导入行更改为import Data.Char hiding (isPunctuation)),导入Data.Char限定的(因此您必须将其函数称为Data.Char.functionName而不是仅functionName),或者为您的函数指定一个不与冲突的名称任何来自Data.Char.

于 2012-12-16T15:17:46.537 回答
0

Data.Char模块有一个名为 的函数isPunctuation。你得到你提到的错误的唯一方法是如果你已经命名了你正在创建的函数。您在这里给出的名称是isPunc,这应该很好,但我认为您实际上使用isPunctuation.了使用不同的名称或使用限定导入:

import qualified Data.Char as Char
于 2012-12-16T16:26:08.403 回答