read
在前奏定义为Haskell中:读取和类型签名
read :: (Read a) => String -> a
和可被用作例如read "1" :: Int
。
现在的功能
readOne :: (Read a) => [String] -> (a, [String])
readOne (x:xs) = (read x,xs)
与readOne ["1","foo"]
结果(如预期)错误
Ambiguous type variable 'a' in the constraint:
'Read a' arising from a use of 'readOne' at :1:0-18
Probable fix: add a type signature that fixes these type variable(s)
但readOne ["1","foo"] :: Int
不工作使用,而
readOneInt :: [String] -> (Int, [String])
readOneInt = readOne
作品就好:
> readOneInt ["1", "foo"]
(1,["foo"])
所以:我怎么可以添加一个类型签名readOne
没有定义像readOneInt
新功能?
非常感谢! – 2010-07-29 12:19:43
小心!最后的例子只能用于ghci,因为1的类型默认为'Integer'。如果你询问ghci表达式的类型(通过':t'),你会得到'(Num a,Read a)=>(a,[String])',它具有与以前相同的问题。 asTypeOf(readOne [“1”,“foo”])(0 :: Double,[] :: [String])' - 我通常在反引号中用asTypeOf写入,但as不喜欢双引号反引号。 – yatima2975 2010-07-29 14:24:53