[Haskell-cafe] Re: A function that makes list of Bool lists from list of Int lists

Ertugrul Soeylemez es at ertes.de
Sun Apr 4 00:45:01 EDT 2010


"Edward Z. Yang" <ezyang at MIT.EDU> wrote:

> Excerpts from Maur Toter's message of Sat Apr 03 09:54:26 -0400 2010:
> > I am new with Haskell so I think this will be a pretty dumb
> > question.  I would like to make a function that makes this:
> >
> > listbool :: [[Int]] -> [[Bool]]
> >
> > in this way:
> > listbool [[1,2],[3,4]] == [[True, True],[False, False]]
> > listbool [[1],[5,5],[5,4],[2]] == [[True],[False, False],[True, True],
> > [False]]
> >
> > So always True from the elements of the first list, False from the
> > elements of the second one, etc...
>
> Sure you can do this.  In fact, the type can be more general:
>
> listbool :: [[a]] -> [[Bool]]
> listbool xs = zipWith ($) (map (\x -> map (const x)) (cycle [True, False])) xs

I prefer to use mapAccumL, which has a somewhat more operational
character:

  listBool :: [[a]] -> [[Bool]]
  listBool = snd . mapAccumL (\t x -> (not t, map (const t) x)) True


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Haskell-Cafe mailing list