[Haskell-cafe] Bathroom reading

Spencer Janssen sjanssen at cse.unl.edu
Tue Aug 14 11:31:51 EDT 2007

On Tuesday 14 August 2007 10:17:53 Dougal Stanton wrote:
> I'm looking for cool but mind-bending examples of functional brilliance.
> Let us say, hypothetically, you had a bathroom without any reading
> material. And having read all the Dilbert and Garfield you could
> seriously stomach, decide you should educate yourself while "on the
> job". :-)
> So you decide to print up some "one-liner" style programs into a
> little booklet. Something between credit-card and postcard sized, with
> a neat but mind-bending program on it. Don Stewart occasionally swoops
> in with some fixpoint malarkey to defuse heated discussions. I mean
> that kind of thing, but with a slightly wider scope than just fibs...
> Suggestions, please!
> D.

Here's a small puzzle: without using a Haskell interpreter, explain what 
the 'foo' function does.

> foo = filterM (const [True, False])

In case you aren't familiar, here's the definition of filterM:

> filterM          :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
> filterM _ []     =  return []
> filterM p (x:xs) =  do
>    flg <- p x
>    ys  <- filterM p xs
>    return (if flg then x:ys else ys)

Spencer Janssen

More information about the Haskell-Cafe mailing list