Page 1 of 1

1 Replies - 1607 Views - Last Post: 30 May 2009 - 05:29 AM

#1 nix64

Reputation: 0
• Posts: 1
• Joined: 15-April 09

Posted 15 April 2009 - 03:34 PM

I am looking at the page seen here

I cannot create the descendingSort function that is shown on that page.

My quicksort function looks like this:
quickSort2 _ [] = []
quickSort2 c (x:xs) =
(quickSort2 c less) ++ (x:equal) ++ (quickSort2 c more)
where less = filter (\y -> y `c` x == LT) xs
equal = filter (\y -> y `c` x == EQ) xs
more = filter (\y -> y `c` x == GT) xs

descending looks like this:
descending x y = compare y x

I put the two together to get the descendingSort function. This is where I get the error.
descendingSort = quickSort2 descending

(Error message from ghci):

Quote

test2.hs:102:28:
Ambiguous type variable `a' in the constraint:
`Ord a' arising from a use of `descending' at test2.hs:102:28-37
Possible cause: the monomorphism restriction applied to the following:
descendingSort :: [a] -> [a] (bound at test2.hs:102:0)
Probable fix: give these definition(s) an explicit type signature
or use -fno-monomorphism-restriction

I don't really understand the error message. As mentioned in the wiki, descendingSort's type signature is supposed to be [a] -> [a], right? What am I doing wrong?

Is This A Good Question/Topic? 0

#2 Raynes

• D.I.C Lover

Reputation: 613
• Posts: 2,815
• Joined: 05-January 09

Posted 30 May 2009 - 05:29 AM

nix64, on 15 Apr, 2009 - 02:34 PM, said:

I am looking at the page seen here

I cannot create the descendingSort function that is shown on that page.

My quicksort function looks like this:
quickSort2 _ [] = []
quickSort2 c (x:xs) =
(quickSort2 c less) ++ (x:equal) ++ (quickSort2 c more)
where less = filter (\y -> y `c` x == LT) xs
equal = filter (\y -> y `c` x == EQ) xs
more = filter (\y -> y `c` x == GT) xs

descending looks like this:
descending x y = compare y x

I put the two together to get the descendingSort function. This is where I get the error.
descendingSort = quickSort2 descending

(Error message from ghci):

Quote

test2.hs:102:28:
Ambiguous type variable `a' in the constraint:
`Ord a' arising from a use of `descending' at test2.hs:102:28-37
Possible cause: the monomorphism restriction applied to the following:
descendingSort :: [a] -> [a] (bound at test2.hs:102:0)
Probable fix: give these definition(s) an explicit type signature
or use -fno-monomorphism-restriction

I don't really understand the error message. As mentioned in the wiki, descendingSort's type signature is supposed to be [a] -> [a], right? What am I doing wrong?

Okay, your problem is something called the monomorphism restriction. There is a good explanation about it here http://book.realworl...s.html#id609960

Now, the easiest thing you can do to fix this is add an explicit type signature. You were close, but the type signature is: (Ord a) => [a] -> [a]

This will clear up any problems.

This post has been edited by Raynes: 30 May 2009 - 05:30 AM