[Haskell-cafe] Re: Practise fingerspelling with Haskell! (Code cleanup request)

J. Garrett Morris trevion at gmail.com
Wed Jul 18 16:45:07 EDT 2007


On 7/18/07, Dougal Stanton <ithika at gmail.com> wrote:
> I worked out that [ (a,b) | a <- as, b <- bs ] must be equivalent to
>
> > comp = concatMap (\x -> map ((,) x) ys) xs
>
> but I can't really say how conditions like "a /= b" get slotted in to
> that style. Is there a reference for that?

As I understand it, list comprehensions are equivalent to monadic
expressions in the [] monad.   The only trick is that conditions in
the list comprehension have to be translated into guard expressions.
For instance,

> [(x,y) | x <- xs, y <- ys, x /= y]

translates into:

> do x <- xs
>   y <- ys
>   guard (x /= y)
>   return (x,y)

You're partway there - concatMap is flip (>>=), so you have the xs >>=
(\x -> <stuff>) part.

 /g

-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.


More information about the Haskell-Cafe mailing list