【发布时间】:2010-11-22 03:24:16
【问题描述】:
我想在 Haskell 函数中包含多个 case 语句(请参阅下面的假设函数示例)。
但是,它不是合法的 Haskell。有什么更好的方法来完成同样的事情?此外,如果 case 语句没有返回任何内容,而只是设置了一些值,那么为什么在一个函数中包含多个 case 语句是不合法的呢?
(我会在第 5 行收到“输入‘case’时出现解析错误”)
tester x y =
case (x < 0) of
True -> "less than zero."
False -> "greater than or equal to zero."
case (y == "foo")
True -> "the name is foo."
False -> "the name is not foo."
请注意,如果我的功能很简单:
tester x y =
case (x < 0) of
True -> "less than zero."
False -> "greater than or equal to zero."
...然后它会编译。
【问题讨论】:
-
一个更详细的例子会有所帮助。如果 x 为 1 且 y 为“foo”,测试器函数应该返回什么?
-
由于 Haskell 中没有副作用,因此 case 表达式设置某个值而不是返回是没有意义的。你想做什么需要超过 1 个案例陈述?