简体   繁体   中英

Can someone explain in detail the signature for this haskell function?

The signature for this function is confusing me and all information online is confusing me. Can someone explain to me the signature of the function and maybe give me an example?

sort3 :: Ord a => (a -> a -> Ordering) -> [a] -> [a]
sort3 cmp xs | length(xs) < 1 = xs

This is the error I'm getting.

Couldn't match expected type ‘a -> a -> Ordering’

with actual type ‘[t0]’

• In the first argument of ‘sort3’, namely ‘[]’
  In the expression: sort3 []
  In an equation for ‘it’: it = sort3 []
• Relevant bindings include
    it :: [a] -> [a] (bound at <interactive>:2:1)

There are two arguments to this function:

sort3 :: Ord a => (a -> a -> Ordering) -> [a] -> [a]

The first argument is itself a function that takes two arguments: an orderable thing and an orderable thing (these are anything which is in the typeclass Ord ) and it returns something of type Ordering .

The second argument is a list of these things, all of which are the exact same orderable thing that the first argument (itself a function) would take two of.

Finally, the sort3 function returns a list of that same orderable thing.

Now, GHCI is telling you that it expects the first argument to be what your signature says it should be (a function that itself takes two arguments and returns an Ordering ), but you passed it an empty list instead:

Couldn't match expected type ‘a -> a -> Ordering’

with actual type ‘[t0]’

• In the first argument of ‘sort3’, namely ‘[]’
  In the expression: sort3 []

In other words: "you told me your first argument would be (a -> a -> Ordering) , but instead you've invoked the function like this sort3 [] and I can't interpret [] as function with this signature: (a -> a -> Ordering) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM