[Haskell-beginners] Mapping list over datatype using Traversable and State monad.

Dmitriy Matrosov sgf.dma at gmail.com
Tue Sep 25 21:06:26 CEST 2012

I have one more question.

Also i need to zipApp list to part of Line: either to only first list
("ordered" elements) or to only second ("other" elements). I have implemented
this using Monoid:

    import Data.Monoid

    instance Monoid (Line a) where
        mempty          = Line [] []
        (Line xs ys) `mappend` (Line xs' ys')
                        = Line (xs `mappend` xs') (ys `mappend` ys')

    onlyOrdered :: Line a -> Line a
    onlyOrdered (Line xs ys)    = Line xs []

    onlyOthers :: Line a -> Line a
    onlyOthers (Line xs ys)     = Line [] ys

and function looks like

    inlineToOrdered :: (a -> a) -> Line a -> Line a
    inlineToOrdered g   = mappend
                            <$> zipApp (id : repeat g) . onlyOrdered
                            <*> onlyOthers

It works as well, but is this solution good? Or there is some better way to
limit "scope" of function application to only part of datatype?

More information about the Beginners mailing list