A function that makes list of Bool lists from list of Int lists

Hey, 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... Can you help me to do that? Thanks for your help! Maur Toter

Am Samstag 03 April 2010 15:54:26 schrieb Maur Toter:
Hey,
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...
Can you help me to do that? Thanks for your help!
Looks like homework, so only a few hints: zipWith, cycle, const
Maur Toter

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 Cheers, Edward

Too many points. listbool :: [[a]] -> [[Bool]] listbool = zipWith ($) (map (map . const) (cycle [True, False])) Cheers, Alex On Saturday 03 April 2010 15:13:48 Edward Z. Yang 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
Cheers, Edward _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hey,
thanks for the help!
Yes it is part of a homework that I can't find out (I am fine with other
parts).
This is not the homework itself, just the part of it and I needed help with
it, thanks for that!
I would like to understand the solution and not only have it so I would like
to ask you:
What does the ($) at zipWith?
Thanks again!
On Sat, Apr 3, 2010 at 6:18 PM, Alexandru Scvortov
Too many points.
listbool :: [[a]] -> [[Bool]] listbool = zipWith ($) (map (map . const) (cycle [True, False]))
Cheers, Alex
On Saturday 03 April 2010 15:13:48 Edward Z. Yang 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
Cheers, Edward _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks again!
The last part I cant understand:
So I give it for example
zipWith ($) (map (\x -> map (const x)) (cycle [True, False])) [[1,2],[3]]
Okay, because of ($) it takes the first element of the last part which is
[1,2] and apply the function on it
But how makes this: map (\x -> map (const x)) (cycle [True, False])
[True,True] from [1,2]?
Then he takes [3] but how makes [False] from that?
It easily can be that my functional programming knowledge isn't enough to
understand this and maybe I misunderstood it already. But I hope I'll be
able to do so..
Thanks:
Maur Toter
so
On Sat, Apr 3, 2010 at 4:31 PM, Edward Z. Yang
Excerpts from Maur Toter's message of Sat Apr 03 10:29:34 -0400 2010:
What does the ($) at zipWith?
($) is function application
Prelude> :t ($) ($) :: (a -> b) -> a -> b
Cheers, Edward

Look at it from the inside, out. What does const do? Const is a function that takes two parameters and always returns the first one. For instance, const True x is True, for all x. What's \x -> map (const x) then? (or map.const in my case) It's a function that takes something and maps all the elements in a list onto that something. i.e. you say 1 [2, 3, 4]; it replies [1, 1, 1]. What's cycle [True, False]? It's simply the list [True, False, True, False, ...]. What's (map (\x -> map (const x)) (cycle [True, False]))? It's a list of functions that map their lists to lists of True or lists of False (alternatively). And what does the zipWith ($) do? It applies the functions we've just built onto the lists you give it. Cheers, Alex On Saturday 03 April 2010 16:34:21 Maur Toter wrote:
Thanks again!
The last part I cant understand: So I give it for example zipWith ($) (map (\x -> map (const x)) (cycle [True, False])) [[1,2],[3]]
Okay, because of ($) it takes the first element of the last part which is [1,2] and apply the function on it But how makes this: map (\x -> map (const x)) (cycle [True, False]) [True,True] from [1,2]? Then he takes [3] but how makes [False] from that?
It easily can be that my functional programming knowledge isn't enough to understand this and maybe I misunderstood it already. But I hope I'll be able to do so..
Thanks: Maur Toter
so
On Sat, Apr 3, 2010 at 4:31 PM, Edward Z. Yang
wrote: Excerpts from Maur Toter's message of Sat Apr 03 10:29:34 -0400 2010:
What does the ($) at zipWith?
($) is function application
Prelude> :t ($) ($) :: (a -> b) -> a -> b
Cheers, Edward

Am Samstag 03 April 2010 19:44:51 schrieb Alexandru Scvortov:
Look at it from the inside, out.
What does const do?
Const is a function that takes two parameters and always returns the first one. For instance, const True x is True, for all x.
What's \x -> map (const x) then? (or map.const in my case)
It's a function that takes something and maps all the elements in a list onto that something. i.e. you say 1 [2, 3, 4]; it replies [1, 1, 1].
What's cycle [True, False]?
It's simply the list [True, False, True, False, ...].
What's (map (\x -> map (const x)) (cycle [True, False]))?
It's a list of functions that map their lists to lists of True or lists of False (alternatively).
And what does the zipWith ($) do?
I dislike the ($) part here, I find it more obvious to write it listbool = zipWith (map . const) (cycle [True,False]) zipWith f (map g xs) ys can always be written as zipWith (f . g) xs ys and usually the latter is much clearer to me. When f is ($), the difference is enormous.
It applies the functions we've just built onto the lists you give it.
Cheers, Alex
On Saturday 03 April 2010 16:34:21 Maur Toter wrote:
Thanks again!
The last part I cant understand: So I give it for example zipWith ($) (map (\x -> map (const x)) (cycle [True, False])) [[1,2],[3]]
Okay, because of ($) it takes the first element of the last part which is [1,2] and apply the function on it But how makes this: map (\x -> map (const x)) (cycle [True, False]) [True,True] from [1,2]? Then he takes [3] but how makes [False] from that?
It easily can be that my functional programming knowledge isn't enough to understand this and maybe I misunderstood it already. But I hope I'll be able to do so..
Thanks: Maur Toter
so
On Sat, Apr 3, 2010 at 4:31 PM, Edward Z. Yang
wrote: Excerpts from Maur Toter's message of Sat Apr 03 10:29:34 -0400 2010:
What does the ($) at zipWith?
($) is function application
Prelude> :t ($) ($) :: (a -> b) -> a -> b
Cheers, Edward

On Sat, Apr 3, 2010 at 7:29 AM, Maur Toter
Hey,
thanks for the help!
Yes it is part of a homework that I can't find out (I am fine with other parts). This is not the homework itself, just the part of it and I needed help with it, thanks for that! I would like to understand the solution and not only have it so I would like to ask you: What does the ($) at zipWith?
There is nothing wrong with asking your question here, but you might get answer that are more appropriate for your level of haskell experience on the haskell-beginner's list: http://haskell.org/mailman/listinfo/beginners When you ask here you might get solutions that do the computation at the type level :) Jason

Alexandru Scvortov
Too many points.
listbool :: [[a]] -> [[Bool]] listbool = zipWith ($) (map (map . const) (cycle [True, False]))
If we choose to zipWith ($), maybe this will do a little less work : listbool = zipWith ($) (cycle $ map (map . const) [True, False]) (ie cycle the 2-element-list of function and not the bool list and then map on an infinite list) -- Guillaume Pinot http://www.irccyn.ec-nantes.fr/~pinot/ « Les grandes personnes ne comprennent jamais rien toutes seules, et c'est fatigant, pour les enfants, de toujours leur donner des explications... » -- Antoine de Saint-Exupéry, Le Petit Prince () ASCII ribbon campaign -- Against HTML e-mail /\ http://www.asciiribbon.org -- Against proprietary attachments

"Edward Z. Yang"
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/
participants (7)
-
Alexandru Scvortov
-
Daniel Fischer
-
Edward Z. Yang
-
Ertugrul Soeylemez
-
Jason Dagit
-
Maur Toter
-
TeXitoi