[Haskell-cafe] Re: Grokking monads by not overlooking concatMap
Achim Schneider
barsoap at web.de
Mon May 5 13:52:24 EDT 2008
Peter Hercek <phercek at gmail.com> wrote:
> Achim Schneider wrote:
> > Peter Hercek <phercek at gmail.com> wrote:
> >>> Haskell wins the wickedness of design contest by using [()] and []
> >>> as truth values.
> >> Maybe you wanted to say: "... by using [()] as True value and [] as
> >> False value" ... which does not seem that wicked (at least to
> >> me).
> >>
> > Strangely enough, it reads exactly the same to me. IMHO, not working
> > like filter is the thing that makes this wicked: guard doesn't care
> > about what is returned, you could as well say (\_ ->
> > [("foo","bar")])
>
> Right, should be a list with one element in it to represent True.
> So maybe it is a bit worse than C since list with two elements
> gives you two True values :-D
> On the other side it is nice that guard works the expected way
> (using the same implementation) with other MonadPlus-es too.
>
Yes ;)
You can actually do stuff like
f'''' = concatMap
(\x -> concatMap
(\y -> concatMap
(\_ -> [(x,y)])
(if x*y == 8 then replicate (x+y) () else []))
[2..8])
[1..4]
, resulting in
[(1,8),(1,8),(1,8),(1,8),(1,8),(1,8),(1,8),(1,8),(1,8),(2,4),(2,4),
(2,4),(2,4),(2,4),(2,4),(4,2),(4,2),(4,2),(4,2),(4,2),(4,2)]
or
Prelude> "Hello World" >>= (\c -> if (c == 'o') then replicate 10 c else [c])
"Helloooooooooo Woooooooooorld"
--
(c) this sig last receiving data processing entity. Inspect headers for
past copyright information. All rights reserved. Unauthorised copying,
hiring, renting, public performance and/or broadcasting of this
signature prohibited.
More information about the Haskell-Cafe
mailing list