dan*_*ara 16 haskell scope pattern-matching where-clause
我知道他们没有跨越模式匹配(即你需要为每个模式重写'where'子句),但是范围如何适用于守卫?
这有用吗?
myFunction x1 x2
| x1 > x2 = addOne x1
| x1 < x2 = addOne x2
| otherwise = x1
where addOne = (1+)
Run Code Online (Sandbox Code Playgroud)
或者应该是这样吗?
myFunction x1 x2
| x1 > x2 = addOne x1
where addOne = (1+)
| x1 < x2 = addOne x2
where addOne = (1+)
| otherwise = x1
Run Code Online (Sandbox Code Playgroud)
Ric*_* T. 16
第一个是正确的.我建议你看看haskell wiki上的let vs where页面,这是一个很好的阅读(它还解释了如何处理范围).就像一个注释,你永远不应该重复相同的定义......这表明你的代码需要以另一种方式构建.