Haskell 嵌套条件
Haskell nested conditions
我在嵌套条件时遇到问题。我是 Haskell 的新手,似乎无法在我的书中或网上找到任何类似的内容。这是我所拥有的示例:
someFunc s n r c e i
| (i < s)
>>> | (e < s) = someFunc (changes go here conditions are met)
| otherwise = createList (different set of conditions go here)
| otherwise = n
给出的错误是:"parse error on input `|'" 在代码中指定的位置。解决这个问题的最佳方法是什么?
感谢英语,抱歉。
你不能那样嵌套守卫,但将它分成两个函数可能更清晰:
someFunc s n r c e i
| (i < s) = innerCondition s n r c e i
| otherwise = n
innerCondition s n r c e i
| (e < s) = someFunc (changes go here conditions are met)
| otherwise = createList (different set of conditions go here)
或者,您可以使用 if
语句将其嵌套在同一函数中:
someFunc s n r c e i
| (i < s) =
if (e < s) then
someFunc (changes go here conditions are met)
else
createList (different set of conditions go here)
| otherwise = n
但我认为第一个带有独立保护函数的例子更清晰。
我在嵌套条件时遇到问题。我是 Haskell 的新手,似乎无法在我的书中或网上找到任何类似的内容。这是我所拥有的示例:
someFunc s n r c e i
| (i < s)
>>> | (e < s) = someFunc (changes go here conditions are met)
| otherwise = createList (different set of conditions go here)
| otherwise = n
给出的错误是:"parse error on input `|'" 在代码中指定的位置。解决这个问题的最佳方法是什么?
感谢英语,抱歉。
你不能那样嵌套守卫,但将它分成两个函数可能更清晰:
someFunc s n r c e i
| (i < s) = innerCondition s n r c e i
| otherwise = n
innerCondition s n r c e i
| (e < s) = someFunc (changes go here conditions are met)
| otherwise = createList (different set of conditions go here)
或者,您可以使用 if
语句将其嵌套在同一函数中:
someFunc s n r c e i
| (i < s) =
if (e < s) then
someFunc (changes go here conditions are met)
else
createList (different set of conditions go here)
| otherwise = n
但我认为第一个带有独立保护函数的例子更清晰。