[Haskell-beginners] Replacing equals with equals not working

martin martin.drautzburg at web.de
Tue Dec 8 22:29:22 UTC 2015


Hello all,

with this code


import Data.List
import Data.Function


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
        (bound at /home/martin/projects/haskell/exercises/99_questions/xxx.hs:10:1)
    Note: there are several potential instances:
      instance Integral a => Ord (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Real’
      instance Ord a => Ord (Control.Applicative.ZipList a)
        -- Defined in ‘Control.Applicative’
      instance Ord Integer -- Defined in ‘integer-gmp:GHC.Integer.Type’
      ...plus 24 others
    In the first argument of ‘on’, namely ‘compare’
    In the expression: compare `on` get
    In an equation for ‘tcmp’: tcmp = compare `on` get

Why is that so?


More information about the Beginners mailing list