【发布时间】:2015-03-07 01:08:33
【问题描述】:
我是 Haskell 的新人。这是我的程序:
maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer
maybe_divide a b = case (a, b) of
(Just a, Just b)
| (Just a, Just b)
| (Nothing, _) -> Nothing
| (_, Just 0) -> Nothing
| (_, Nothing) -> Nothing
| (Just a,Just b) -> Just (a `div` b)
编译器显示:表达式上下文中的模式语法:_。 当我只使用没有防护装置的情况时,它可以工作。为什么它在守卫中不起作用?
【问题讨论】:
-
守卫用于进一步限制模式:在上面的示例中您不需要守卫。那么...您到底想获得什么?
-
删除所有管道字符 (
|),您的代码将编译。 -
@chi,这看起来像是在尝试在 Haskell 中使用 ML 语法。
-
@dfeuer 我也是这么想的,但是关于守卫与模式的评论让我很困惑。
标签: haskell case pattern-guards