为什么Haskell在理解中不允许模式匹配?

时间:2018-11-18 19:28:42

标签: list haskell scope list-comprehension monads

我编写了以下(平凡的)函数:

h c = [f x | x <- a, f <- b, (a, b) <- c]

我希望这会因为:

h c = do (a, b) <- c
         f <- b
         x <- a
         return (f x)

反之,将其删除(忽略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]

这似乎是荒谬的,因为ab确实在范围之内。

1 个答案:

答案 0 :(得分:12)

您的绑定顺序错误。

select p.ParFirst, p.ParLast, c1.ChildFirst, c2.ChildFirst, c3.ChildFirst
from dbo.Parents p
outer apply ( select ChildFirst from (select ChildFirst, row_number() over  (order by ChildFirst) as rowNo from dbo.Children c where c.ParentId = p.ParentId) t1 where t1.rowNo = 1 )  c1
outer apply ( select ChildFirst from (select ChildFirst, row_number() over(order by ChildFirst) as rowNo from dbo.Children c where c.ParentId = p.ParentId) t2 where t2.rowNo = 2 )  c2
outer apply ( select ChildFirst from (select ChildFirst, row_number() over(order by ChildFirst) as rowNo from dbo.Children c where c.ParentId = p.ParentId) t3 where t3.rowNo = 3 )  c3