-4

I'm told to write a function that would check if the punctuations in a string are all spaces. i.e.:

haskell> f "Hello my name is Brad"
True

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

I wrote an auxiliary function as follows,

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

which was accepted by Haskell and worked fine. But as soon as I used it in the following function,

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

it gives me this error:

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

I am partially getting what it's complaining about, but having imported Data.Char, I don't really see why it's complaining.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Brad
  • 133
  • 3
  • @minitech Is there though? There is no function with that name in my installation and it's not listed in the docs either (though `isPunctuation` is). Note that the OP's code works fine for me. – sepp2k Dec 16 '12 at 15:24
  • 5
    Brad, did you post your code and the error message exactly as-is? You didn't name your function `isPunctuation` in your real code and then renamed it to `isPunc` in the code and in the error message when you posted it here, did you? – sepp2k Dec 16 '12 at 15:27

2 Answers2

4

(This post is written under the assumption that you really named your function isPunctuation, not isPunc)

It's ambiguous because Haskell doesn't know whether you're referring to your own isPunctuation function (Main.isPunctuation) or Data.Char's isPunctuation function when you call isPunctuation. It's ambiguous because you imported Data.Char - if you hadn't imported it or if you imported it qualified, there would be no ambiguity as isPunctuation could only refer to Main.isPunctuation.

To fix the ambiguity, either don't import isPunctuation from Data.Char (by changing the import line to import Data.Char hiding (isPunctuation)), import Data.Char qualified (so you have to refer to its functions as Data.Char.functionName rather than just functionName), or give your function a different name that doesn't conflict with anything from Data.Char.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
0

Data.Char module has a function by the name isPunctuation. The only way you are getting the error you mentioned is if you had named the function you were creating the same. The name you have given here is isPunc, which should be perfectly fine, but I think you actually used isPunctuation. Use a different name or use qualified import:

import qualified Data.Char as Char
manojlds
  • 290,304
  • 63
  • 469
  • 417