【发布时间】:2016-12-30 02:31:07
【问题描述】:
所以我试图匹配这个模式。 MessageType 是我创建的一种类型。此函数接收一个字符串,然后根据该字符串的第一个字符,它输出一个 MessageType。每当我编译时,我都会收到一条警告:
Pattern match(es) are non-exhaustive
In an equation for ‘parseMessage’: Patterns not matched: []
这是我的代码:
parseMessage :: String -> MessageType
parseMessage (x:_)
| x == 'I' = Info
| x == 'W' = Warning
| otherwise = Error 1
为什么我的模式匹配并不详尽? otherwise 守卫不会抓到别的东西吗?我看不到我的函数如何无法捕获所有字符串。
当我这样编写函数时,我没有收到警告。
parseMessage [] = error "Empty String"
parseMessage (x:_) = if x == 'I'
then Info
else if x == 'W'
then Warning
else Error 1
我以这种方式重写了我的函数,因为我看到警告说“模式不匹配:[]”,所以我明确地处理了它。但是为什么在我的函数的第一个版本中,它说模式没有被处理。 otherwise怎么除了前两个守卫什么都抓不到?
【问题讨论】:
标签: haskell