Haskell嵌套条件
问题描述:
我遇到了嵌套它们的条件问题。我是Haskell的新手,似乎无法在我的书或网上找到类似的东西。这里是什么,我有一个例子:被赋予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
的错误是:“输入解析错误'|'”在代码表示的点。怎样才能解决这个问题?
感谢和抱歉的英语。
答
您不能嵌套守卫这种方式,但它可能是吸尘器,它像这样分成两个功能:或者
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
但我认为具有单独的保护功能的第一个例子是更清洁。
另请注意,如果内部条件不是详尽无遗(例如,不以“其他方式”或等效方式结束),那么程序可能会崩溃而无法像对待外部条件那样将控制权返回给外部条件。 – chi