[英]Why does Haskell not allow pattern matching in comprehensions?
我写了以下(琐碎)函数:
h c = [f x | x <- a, f <- b, (a, b) <- c]
我本以为这是因为:
h c = do (a, b) <- c
f <- b
x <- a
return (f x)
反过来,desugared(忽略fail
东西):
h c = c >>= \(a, b) -> b >>= \f -> a >>= \x -> return (f x)
但是,GHCi会返回错误:
<interactive>:24:17: error: Variable not in scope: a :: [a1]
<interactive>:20:27: error:
Variable not in scope: b :: [t0 -> b1]
这似乎是荒谬的,因为a
和b
确实在范围内。
您的绑定顺序错误。
h c = [f x | (a,b) <- c, f <- b, x <- a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.