[Haskell-cafe] Default implementation of type class member function not working as expected.

Sylvain Henry hsyl20 at gmail.com
Wed May 21 18:04:34 UTC 2014


Are you trying to do something like that?

data Rose a = Rose a [Rose a]

class TreeLike t where
   getNodes :: t a -> [t a]

   getAllNodes :: t a -> [t a]
   getAllNodes t = getNodes t ++ (concatMap getAllNodes (getNodes t))

instance TreeLike Rose where
   getNodes (Rose _ xs) = xs


-Sylvain


2014-05-21 15:12 GMT+02:00 David Banas <capn.freako at gmail.com>:

> That worked, Brandon; thanks!
>
> Gurus, I’d still love to understand exactly what I broke, trying to do it
> the other way. Any thoughts?
>
> Thanks,
> -db
>
> On May 21, 2014, at 12:14 AM, haskell-cafe-request at haskell.org wrote:
>
> if there's only one definition, then define it
> *outside* the typeclass.
>
>
>
> I have a typeclass, which defines the following two member functions:
> (t is a Rose Tree.)
>
>    getCompNodes   :: t -> [CompNode a]
>
>    getAllCompNodes :: t -> [CompNode a]
>    getAllCompNodes t = getCompNodes t
>                     ++ (concatMap getAllCompNodes (subForest t))
>
>
> The first one must be defined uniquely by each instance, but the second
> never needs a unique definition.
> So, I provided its implementation in the typeclass definition, as shown.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140521/c7976c3e/attachment.html>


More information about the Haskell-Cafe mailing list