是否可以在守卫中匹配布尔元组,如模式匹配?
Can a tuple of booleans be matched in guards, like pattern matching?
我尝试创建一个 returns 不同字符串的函数,具体取决于布尔元组中的值
chooseAction :: (Bool , Bool , Bool , Bool) -> String
chooseAction (isJump ,isAcceleration ,isDeceleration ,isSpeedOk)
| (True ,False, False, False) = "JUMP"
| (False ,True, False, False) = "SPEED"
| (False ,False, True, False) = "SLOW"
| (False ,False ,False, True) = "WAIT"
但是这不能编译:
Couldn't match expected type `Bool'
with actual type `(Bool, Bool, Bool, Bool)'
In the expression: (True, False, False, False)
In a stmt of a pattern guard for
an equation for `chooseAction':
(True, False, False, False)
In an equation for `chooseAction':
chooseAction (isJump, isAcceleration, isDeceleration, isSpeedOk)
| (True, False, False, False) = "JUMP"
| (False, True, False, False) = "SPEED"
| (False, False, True, False) = "SLOW"
| (False, False, False, True) = "WAIT"
我在定义这样一个函数时做错了什么?
为此使用模式匹配:
chooseAction :: (Bool , Bool , Bool , Bool) -> String
chooseAction (True ,False, False, False) = "JUMP"
chooseAction (False ,True, False, False) = "SPEED"
chooseAction (False ,False, True, False) = "SLOW"
chooseAction (False ,False ,False, True) = "WAIT"
chooseAction _ = error "This should not happen" -- if sequence is something else
守卫表达式的计算结果必须是 Bool
值,但在您的情况下,您有布尔元组。这就是它抛出错误的原因
Couldn't match expected type `Bool'
with actual type `(Bool, Bool, Bool, Bool)'
你最好像这样定义函数
chooseAction::(Bool, Bool, Bool, Bool) -> String
chooseAction (True, False, False, False) = "JUMP"
chooseAction (False, True, False, False) = "SPEED"
chooseAction (False, False, True, False) = "SLOW"
chooseAction (False, False, False, True) = "WAIT"
此外,您并未涵盖所有情况。所以,你可以这样覆盖
chooseAction tuple_of_bools = "NONE"
如果none个模式匹配,则执行最后一个得到NONE
。或者,如果你希望它像现在这样定义,那么只需更改比较,就像这样
chooseAction:: (Bool, Bool, Bool, Bool) -> String
chooseAction bools
| bools == (True ,False, False, False) = "JUMP"
| bools == (False ,True, False, False) = "SPEED"
| bools == (False ,False, True, False) = "SLOW"
| bools == (False ,False ,False, True) = "WAIT"
| otherwise = "NONE"
我尝试创建一个 returns 不同字符串的函数,具体取决于布尔元组中的值
chooseAction :: (Bool , Bool , Bool , Bool) -> String
chooseAction (isJump ,isAcceleration ,isDeceleration ,isSpeedOk)
| (True ,False, False, False) = "JUMP"
| (False ,True, False, False) = "SPEED"
| (False ,False, True, False) = "SLOW"
| (False ,False ,False, True) = "WAIT"
但是这不能编译:
Couldn't match expected type `Bool' with actual type `(Bool, Bool, Bool, Bool)' In the expression: (True, False, False, False) In a stmt of a pattern guard for an equation for `chooseAction': (True, False, False, False) In an equation for `chooseAction': chooseAction (isJump, isAcceleration, isDeceleration, isSpeedOk) | (True, False, False, False) = "JUMP" | (False, True, False, False) = "SPEED" | (False, False, True, False) = "SLOW" | (False, False, False, True) = "WAIT"
我在定义这样一个函数时做错了什么?
为此使用模式匹配:
chooseAction :: (Bool , Bool , Bool , Bool) -> String
chooseAction (True ,False, False, False) = "JUMP"
chooseAction (False ,True, False, False) = "SPEED"
chooseAction (False ,False, True, False) = "SLOW"
chooseAction (False ,False ,False, True) = "WAIT"
chooseAction _ = error "This should not happen" -- if sequence is something else
守卫表达式的计算结果必须是 Bool
值,但在您的情况下,您有布尔元组。这就是它抛出错误的原因
Couldn't match expected type `Bool'
with actual type `(Bool, Bool, Bool, Bool)'
你最好像这样定义函数
chooseAction::(Bool, Bool, Bool, Bool) -> String
chooseAction (True, False, False, False) = "JUMP"
chooseAction (False, True, False, False) = "SPEED"
chooseAction (False, False, True, False) = "SLOW"
chooseAction (False, False, False, True) = "WAIT"
此外,您并未涵盖所有情况。所以,你可以这样覆盖
chooseAction tuple_of_bools = "NONE"
如果none个模式匹配,则执行最后一个得到NONE
。或者,如果你希望它像现在这样定义,那么只需更改比较,就像这样
chooseAction:: (Bool, Bool, Bool, Bool) -> String
chooseAction bools
| bools == (True ,False, False, False) = "JUMP"
| bools == (False ,True, False, False) = "SPEED"
| bools == (False ,False, True, False) = "SLOW"
| bools == (False ,False ,False, True) = "WAIT"
| otherwise = "NONE"