[Haskell-beginners] Replacing equals with equals not working

Henk-Jan van Tuyl hjgtuyl at chello.nl
Wed Dec 9 00:13:32 UTC 2015


On Tue, 08 Dec 2015 23:29:22 +0100, martin <martin.drautzburg at web.de>  
wrote:
:
> data Tree a = B a (Tree a) (Tree a) | L a Char deriving Show
>
> get (B a _ _) = a
> get (L a _) = a
>
> tcmp = compare `on` get
>
> build :: [Tree Int] -> [Tree Int]
> build (t:[]) = [t]
> -- build t = let xs = sortBy (compare `on` get) t -- < --
> build t = let xs = sortBy tcmp t
>           in build (merge (xs!!0) (xs!!1) : drop 2 xs)
>
>
> The commented line -- < -- does not work, though I am just replacing  
> equals with equals. I get
>
>     No instance for (Ord b0) arising from a use of ‘compare’
>     The type variable ‘b0’ is ambiguous
>     Relevant bindings include
>       tcmp :: Tree b0 -> Tree b0 -> Ordering
:
> Why is that so?

If you do not use the function tcmp, the compiler can not deduce the type  
of it. When you add a type like
   tcmp :: Ord a => Tree a -> Tree a -> Ordering
, the compiler has enough information to compile the program.

Regards,
Henk-Jan van Tuyl


-- 
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


More information about the Beginners mailing list