Haskell读取没有解析器错误

时间:2015-03-27 01:30:41

标签: haskell

我有这行代码,但它有一些问题..

type PhonePad = [(Int,String)]
dayPad = [(1,"Monday"), (2,"Tuesday")] -- I don't list out all of them
data PhoneDay = Monday | Tuesday
    deriving (Eq, Ord,Show,Read,Bounded)
testtt :: PhonePad -> Int -> String
testtt p d = snd $ (filter (\x -> fst x == d) p)!!0

我想显示号码的PhonePad类型。说testtt dayPad 2 == "Tuesday" 我想得到Tuesday

read $ testtt dayPad 2 :: PhonePad

但它始终返回错误消息:

*** Exception: Prelude.read: no parse

有谁知道这里有什么问题?

1 个答案:

答案 0 :(得分:5)

Tuesday不是PhonePad,而是PhoneDay。尝试将字符串"Tuesday"作为type PhonePad = [(Int,String)]读取会导致错误,因为它的格式不像IntString的元组列表。

Prelude> (read $ testtt dayPad 2) :: PhonePad
*** Exception: Prelude.read: no parse

testtt dayPad 2 == "Tuesday"

如果您尝试将字符串"Tuesday"作为PhoneDay读取,则可以正常工作。

Prelude> (read $ testtt dayPad 2) :: PhoneDay
Tuesday