[Haskell-cafe] Function that put elements in Left or Right side of Either depending on type

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Thu Mar 3 15:13:40 UTC 2016


On Thu, Mar 03, 2016 at 04:04:07PM +0100, Kristoffer Føllesdal wrote:
> I want a function fun :: q -> Either a b where q is of type a or b.  fun shall work in the following way
> 
> fun x    gives    Left x     if x :: a
> fun x    gives    Right x   if x :: b

I can't help saying again that, despite this being a fine thing to *want*,
Haskell really doesn't do this kind of polymorphism very well.  If you
pursue this road much further you will end up with

  1. a deeper understanding of how Haskell's typeclasses work and why what
     you are trying to do just doesn't work very well, but

  2. no useful code

If you want outcome 1 then feel free to keep going.  If not I suggest you
just use differently named functions for the 'a' and 'Vect k a' cases.  In
my experience it really is *by far* the simplest solution.

Tom


More information about the Haskell-Cafe mailing list