Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-beginners] Design Question: fmap with two lists

1 view
Skip to first unread message

jonathan...@gmail.com

unread,
Dec 24, 2009, 5:22:10 PM12/24/09
to begi...@haskell.org
I have a class that is basically:

data TwoLists = TL {doubleList :: [Double], intList :: [Int]}

Almost all of the operations of this type map, fold, etc on one of the
lists or the other, and then return a new instance of the type. So I am
implementing to functions dFmap, and iFmap. I actually would like all of
the list functions, but I don't want to reimplement them.

So my question is, what is the haskell way of handling a type like this. Is
there some sort of TwoTypeTraversable I should be deriving from? Essential
is there a Haskell idiom for this situation.

-Jonathan

Maciej Piechotka

unread,
Dec 24, 2009, 7:48:59 PM12/24/09
to begi...@haskell.org
On Thu, 2009-12-24 at 22:21 +0000, jonathan...@gmail.com wrote:
> I have a class that is basically:
>

Hmm. Class in haskell means something similar to interface in OOP. Yes
it is confusing.

> data TwoLists = TL {doubleList :: [Double], intList :: [Int]}
>
> Almost all of the operations of this type map, fold, etc on one of the
> lists or the other, and then return a new instance of the type.

It may be my level of English bit I cannot understend this sentence.

> So I am implementing to functions dFmap, and iFmap.

What is dFmap and iFmap?

> I actually would like all of the list functions, but I don't want to
> reimplement them.
>

Hmm. If you need to combine the lists I'd suggest zip/zipWith. If simply
map/fold over one then just write (or similar):

foldr f i . doubleList

If you mean if there is class:
class BiFunctor f where
fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d
fmap2 f g = fmap2b g . fmap2a f
fmap2a :: (a -> c) -> f a b -> f c b
fmap2a f = fmap2 f id
fmap2b :: (b -> d) -> f a b -> f a d
fmap2b = fmap2 id

To write:
data TwoLists a b = TwoLists [a] [b]
instance BiFunctor TwoLists where
fmap2 f g (TwoLists a b) = TwoLists (map f a) (map g b)
then as far as I know - no - but you can always write your own.

Regards


_______________________________________________
Beginners mailing list
Begi...@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

Brent Yorgey

unread,
Dec 25, 2009, 9:10:30 PM12/25/09
to begi...@haskell.org
On Fri, Dec 25, 2009 at 01:48:25AM +0100, Maciej Piechotka wrote:
> On Thu, 2009-12-24 at 22:21 +0000, jonathan...@gmail.com wrote:
> > I have a class that is basically:
> >
>
> Hmm. Class in haskell means something similar to interface in OOP. Yes
> it is confusing.
>
> > data TwoLists = TL {doubleList :: [Double], intList :: [Int]}
> >
> > Almost all of the operations of this type map, fold, etc on one of the
> > lists or the other, and then return a new instance of the type.
>
> It may be my level of English bit I cannot understend this sentence.
>
> > So I am implementing to functions dFmap, and iFmap.
>
> What is dFmap and iFmap?
>
> > I actually would like all of the list functions, but I don't want to
> > reimplement them.
> >
>
> Hmm. If you need to combine the lists I'd suggest zip/zipWith. If simply
> map/fold over one then just write (or similar):
>
> foldr f i . doubleList
>
> If you mean if there is class:
> class BiFunctor f where
> fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d
> fmap2 f g = fmap2b g . fmap2a f
> fmap2a :: (a -> c) -> f a b -> f c b
> fmap2a f = fmap2 f id
> fmap2b :: (b -> d) -> f a b -> f a d
> fmap2b = fmap2 id

There is such a class in the category-extras package, although it is
so general that it is probably not worth the effort of trying to
understand it unless you really need the extra abstraction.

-Brent

Maciej Piechotka

unread,
Dec 26, 2009, 12:12:23 PM12/26/09
to begi...@haskell.org
On Fri, 2009-12-25 at 21:10 -0500, Brent Yorgey wrote:
> There is such a class in the category-extras package, although it is
> so general that it is probably not worth the effort of trying to
> understand it unless you really need the extra abstraction.
>
> -Brent

For the record - what is the name of it? I tried to search through
category-extras twice.

Regards

Brent Yorgey

unread,
Dec 27, 2009, 9:07:48 AM12/27/09
to begi...@haskell.org
On Sat, Dec 26, 2009 at 06:12:00PM +0100, Maciej Piechotka wrote:
> On Fri, 2009-12-25 at 21:10 -0500, Brent Yorgey wrote:
> > There is such a class in the category-extras package, although it is
> > so general that it is probably not worth the effort of trying to
> > understand it unless you really need the extra abstraction.
> >
> > -Brent
>
> For the record - what is the name of it? I tried to search through
> category-extras twice.

Control.Functor.Bifunctor. It took me a while to find it, too (I've
had to re-find it several times); I didn't actually say where to find
it in my first email because I knew it would take me a long time.
Unfortunately, coherent organization is not among category-extras'
strengths. ;-)

-Brent

0 new messages