Haskell 是否可以在警卫中使用警卫?

Haskell 是否可以在警卫中使用警卫?,haskell,Haskell,我想知道是否有可能在haskell的守卫中使用守卫。 像这样: analyser modele instr | instr == NoCom = (modele, "No command" ,[]) | instr == ComK | (read comargstr1) == 0 = function1 modele Nothing | (read comargstr1) == 1 = function1 modele (Just (r

我想知道是否有可能在haskell的守卫中使用守卫。 像这样:

analyser modele instr
  |  instr == NoCom  = (modele, "No command" ,[])
  |  instr == ComK   | (read comargstr1) == 0 = function1 modele Nothing
                     | (read comargstr1) == 1 = function1 modele (Just (read comargstr1))
                     | (read comargstr1) <  0 = function1 modele (Just (read comargstr2))
                     | otherwise              = function2 modele
  | othercases...
  | othercases...
分析仪型号仪表
|instr==NoCom=(modele,“无命令”,[])
|instr==ComK |(读取comargstr1)==0=function1 modele Nothing
|(读取comargstr1)==1=功能1模型(仅读取comargstr1))
|(读取comargstr1)<0=功能1模型(仅读取comargstr2))
|否则=函数2 modele
|其他情况。。。
|其他情况。。。
在我的示例中,我根本无法在guards的第一列中计算(读取comargstr1),因为comargstr1并不总是返回一个可读取的兼容字符串(致命错误)

我没有设法在警卫中使用警卫

这是可能的(是否有一个技巧,一个选择,一些特别的东西,…),还是完全不可能


提前感谢您的帮助

布局不适用于防护装置,所以如何对齐它们无关紧要


你能做的最接近的方法是用于二级防护。

只需重复这两个条件,或者在单个防护之后使用
(如果
)。据我所知,不支持两个/嵌套的防护

你可以实现如下类似的目标:

analyser modele instr
  |  instr == NoCom = (modele, "No command" ,[])
  |  instr == ComK  = foo
  | othercases...
  | othercases...
  where foo | (read comargstr1) == 0 = function1 modele Nothing
            | (read comargstr1) == 1 = function1 modele (Just (read comargstr1))
            | (read comargstr1) <  0 = function1 modele (Just (read comargstr2))
            | otherwise              = function2 modele
分析仪型号仪表
|instr==NoCom=(modele,“无命令”,[])
|仪表==ComK=foo
|其他情况。。。
|其他情况。。。
其中foo |(读取comargstr1)==0=function1 modele Nothing
|(读取comargstr1)==1=功能1模型(仅读取comargstr1))
|(读取comargstr1)<0=功能1模型(仅读取comargstr2))
|否则=函数2 modele

请注意,每个分支都需要一个不同的名称
foo
。此外,如果嵌套的保护列表没有以
结尾,否则
(或等效项),则控制权将不会转移到最顶层的下一个保护。

是否存在不使用模式匹配的原因?我会写
analyzermodelenocom=;analyzer modele ComK=案例(读取comargstr1){0->f1 modele Nothing;1->f1 modele(仅1);a1 | a1<0->f1 modele(仅读取comargstr2));否则->f2 modele
(实际上,我会先将参数解析为自定义数据类型,然后根据该数据类型进行调度,但至少解析部分会以类似的方式开始…)