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

Jorden Mauro jrm8005 at gmail.com
Thu Jan 28 16:21:56 EST 2010


The syntax is thus:

mysort :: (Ord a) => [a] -> [a]

On Thu, Jan 28, 2010 at 4:06 PM, george young <georgeryoung at gmail.com> wrote:
> [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]
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list