[Haskell-beginners] how to specify type class in type of function?

george young georgeryoung at gmail.com
Thu Jan 28 16:06:36 EST 2010


[complete newbie using ghci 6.10.4
I understand that I should indicate that 'a' is in class Ord, since I use
lessthan.  But what is the syntax?  Other style suggestions are welcome.  

mysort :: [a] -> [a]
mysort [] = []
mysort [x] = [x]
mysort [x, y] | (x <= y) = [x, y]
              | otherwise = [y, x]           
mysort l = 
  (mysort s1) ++ (mysort s2)
    where
      (s1, s2) = splitAt (div (length l) 2) l

Could not deduce (Ord a) from the context ()
      arising from a use of `<=' at /home/gry/foo.hs:53:17-22
    Possible fix:
      add (Ord a) to the context of the type signature for `mysort'
    In the expression: (x <= y)
    In a stmt of a pattern guard for
                 the definition of `mysort':
        (x <= y)
    In the definition of `mysort':
        mysort [x, y]
                 | (x <= y) = [x, y]
                 | otherwise = [y, x]



More information about the Beginners mailing list