[Haskell-beginners] Functor instance for ordered lists

martin martin.drautzburg at web.de
Mon Jan 4 14:26:19 UTC 2016


Am 01/04/2016 um 11:45 AM schrieb Imants Cekusins:
>> a newtype for ordered lists
> 
> why not:
> newtype Ordlist a = Ordlist [a]

All nice and dandy, but at first you already need an Ord constraint for your smart constructor

-- and a ctor:
ordList::(Ord a) => [a]-> OrdList a
ordList = OrdList . sort

but this is still not the main problem. When you try to define a Functor instance, you'd be tempted to do this (at least
I was):

instance Functor OrdList where
        fmap f (OrdList xs) = OrdList $ sort $ map f xs

but you can't do this, because of: "No instance for (Ord b) arising from a use of ‘sort’", where b is the return type of
f :: (a->b). This does make sense, the function has to return something which can be sorted.

So my question is: is it impossible to write a functor instance for ordered lists? It appears so, because a Functor does
not impose any constraints of f. But my knowledge is quite limited and maybe a well-set class constraint can fix things.




More information about the Beginners mailing list