schuelermine
schuelermine

Reputation: 2290

Why does Haskell not allow pattern matching in comprehensions?

I've written the following (trivial) function:

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

I'd've expected this to be desugared as:

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

In turn, desugared (ignoring the fail stuff) as:

h c = c >>= \(a, b) -> b >>= \f -> a >>= \x -> return (f x)

However, GHCi returns the error:

<interactive>:24:17: error: Variable not in scope: a :: [a1]
<interactive>:20:27: error:
    Variable not in scope: b :: [t0 -> b1]

This seems nonsensical, as a and b are indeed in scope.

Upvotes: 3

Views: 136

Answers (1)

Your bindings are in the wrong order.

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

Upvotes: 12

Related Questions