简体   繁体   English

有人可以详细解释此haskell函数的签名吗?

[英]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 . 第一个参数本身就是一个function有两个参数:一个订购的东西和事情订购(这是什么,这是在类型类Ord ),它返回一个类型的东西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. 第二个参数是这些东西的list ,所有这些都是第一个参数(本身是一个函数)将两个完全相同的可list事物。

Finally, the sort3 function returns a list of that same orderable thing. 最后, sort3函数返回相同可sort3的列表。

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: 现在,GHCI告诉您它希望第一个参数是您的签名应具有的含义(一个函数本身带有两个参数并返回Ordering ),但是您却向其传递了一个空列表:

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) . 换句话说:“您告诉我您的第一个参数是(a -> a -> Ordering) sort3 [] (a -> a -> Ordering) ,但是相反,您已经调用了诸如sort3 []这样的函数,而我无法将[]解释为具有此签名的函数: (a -> a -> Ordering)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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