1

I have one problem. I want to implement function which will compare two nodes and give ordering,where nodes the following structure:

data Node a = Node { label :: a, adjacent :: [(a,Int)] } deriving Show


sortNode:: Node a->Node a->Ordering
sortNode node1 node2
    | takeLabel node1 > takeLabel node2 = GT
    | takeLabel node1 < takeLabel node2 = LT
    | takeLabel node1 == takeLabel node2 = EQ

hugs complain about this

ERROR "Network.hs":35 - Cannot justify constraints in explicitly typed binding
*** Expression    : sortNode
*** Type          : Node a -> Node a -> Ordering
*** Given context : ()
*** Constraints   : Ord a

can you explain?(i am beginner in haskell)

4

1 回答 1

3

Your type signature for sortNode specifies an empty context but (<) and (>) require that type a is an instance of class Ord and (==) requires that it is an instance of class Eq. You need to add a constraint to the context as Nikita suggested or omit the explicit type signature and the interpreter will be able to infer it.

sortNode :: (Ord a) => Node a -> Node a -> Ordering
于 2013-04-24T13:47:49.520 回答