[Haskell-cafe] A function that makes list of Bool lists from list
of Int lists
Daniel Fischer
daniel.is.fischer at web.de
Sat Apr 3 15:10:19 EDT 2010
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 <ezyang at mit.edu> 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
More information about the Haskell-Cafe
mailing list