[Haskell-beginners] Return a Foldable instance

Antoine Latter aslatter at gmail.com
Wed May 4 13:32:48 CEST 2011


On May 4, 2011 1:33 AM, "Federico Mastellone" <fmaste at gmail.com> wrote:
>
> Sorry, the function type was incorrect, is for a part of the multimap,
> not the entire multimap. But it's the same problem, a function that
> returns a Foldable.
>
> getValues :: Foldable f => k -> MultiMap k v -> f v
>
> When the user asks for the values that a certain key contains I want
> to return something that is generic and easy to play with while also
> taking advantages of the underlying structure functions to avoid
> repeated code.

Well, "generic and easy to play with" is pretty much the definition of a
list.

If the only thing the caller is doing with the list is folding over it, the
compiler should get rid of the intermediary list anyway, if things work out
right.

So I think you're worrying too soon.

But if this something you want to do anyway, I still don't see what is wrong
with:

> newtype LookupResult a = L [a]
>     deriving (Foldable)

Then when you change your implementation of the multimap you can change your
newtype without affecting the caller.

Antoine

>
> On Wed, May 4, 2011 at 2:51 AM, Chaddaï Fouché <chaddai.fouche at gmail.com>
wrote:
> > On Wed, May 4, 2011 at 7:18 AM, Federico Mastellone <fmaste at gmail.com>
wrote:
> >> Yes, I got confused between haskell type classes and OO classes.
> >>
> >> I want to create different implementations of a multimap, for example
> >> using lists and using Data.Set, and instead of providing functions
> >> getValuesList and getValuesSet that return a [] and a Set respectively
> >> I want to provide a more generic one, getValues that returns a
> >> Foldable and avoid unnecessary conversions to and from lists. This way
> >> the user folds directly the underlying structure, without having to
> >> worry about which function is the best to fold the values. But I don't
> >> know how to do this without an extra intermediate data type.
> >>
> >> One implementation returns a list and the other a set, both Foldables.
> >> But I can't make a function like the one below for this two
> >> getValues :: Foldable f => MultiMap k v -> f v
> >>
> >
> > Writing this function given your intention (doing different thing
> > depending on the type) is equivalent to writing an instance of
> > Foldable for your (MultiMap k). If the underlying structure of your
> > multimaps is always foldable, it doesn't make sense to make a function
> > like this, the multimaps themselves should be Foldable. This is not
> > hard to do, refer to the doc (
> >
http://www.haskell.org/ghc/docs/7.0-latest/html/libraries/base-4.3.1.0/Data-Foldable.html
> > ) to see how to write a minimal instance of Foldable.
> >
> > --
> > Jedaï
> >
>
>
>
> --
> Federico Mastellone
> Computer Science Engineer - ITBA
> ".. there are two ways of constructing a software design: One way is
> to make it so simple that there are obviously no deficiencies, and the
> other way is to make it so complicated that there are no obvious
> deficiencies. The first method is far more difficult."
>
> Tony Hoare, 1980 ACM Turing Award Lecture.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110504/cee04878/attachment.htm>


More information about the Beginners mailing list