我有这行代码,但它有一些问题..
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
有谁知道这里有什么问题?
答案 0 :(得分:5)
Tuesday
不是PhonePad
,而是PhoneDay
。尝试将字符串"Tuesday"
作为type PhonePad = [(Int,String)]
读取会导致错误,因为它的格式不像Int
和String
的元组列表。
Prelude> (read $ testtt dayPad 2) :: PhonePad
*** Exception: Prelude.read: no parse
(testtt dayPad 2 == "Tuesday"
)
如果您尝试将字符串"Tuesday"
作为PhoneDay
读取,则可以正常工作。
Prelude> (read $ testtt dayPad 2) :: PhoneDay
Tuesday