Haskell 哈斯克尔';不能';t将预期类型与实际类型匹配';

Haskell 哈斯克尔';不能';t将预期类型与实际类型匹配';,haskell,types,ghci,Haskell,Types,Ghci,我在七周的时间里学习了七种语言,我正在和哈斯克尔一起工作 我正在努力解决这个问题: 编写一个接受列表的排序和一个比较其两个参数的函数,然后返回一个排序列表 我在网上搜索了帮助并找到了一个解决方案,但由于预期的实际类型错误,我甚至无法运行该解决方案 以下是我一直在尝试的代码: module Main where import Data.List sortList :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a] sort

我在七周的时间里学习了七种语言,我正在和哈斯克尔一起工作

我正在努力解决这个问题:

编写一个接受列表的排序和一个比较其两个参数的函数,然后返回一个排序列表

我在网上搜索了帮助并找到了一个解决方案,但由于预期的实际类型错误,我甚至无法运行该解决方案

以下是我一直在尝试的代码:

module Main where
import Data.List
sortList :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a]
sortList comparator list = sortBy comparator list
*Main> sortList [5,4,2,7,8,1]

<interactive>:1:10:
    Couldn't match expected type `a -> a -> Ordering'
                with actual type `[t]'
    In the first argument of `sortList', namely `[5, 4, 2, 7, ....]'
    In the expression: sortList [5, 4, 2, 7, ....]
    In an equation for `it': it = sortList [5, 4, 2, ....]
错误如下:

module Main where
import Data.List
sortList :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a]
sortList comparator list = sortBy comparator list
*Main> sortList [5,4,2,7,8,1]

<interactive>:1:10:
    Couldn't match expected type `a -> a -> Ordering'
                with actual type `[t]'
    In the first argument of `sortList', namely `[5, 4, 2, 7, ....]'
    In the expression: sortList [5, 4, 2, 7, ....]
    In an equation for `it': it = sortList [5, 4, 2, ....]
*Main>排序列表[5,4,2,7,8,1]
:1:10:
无法匹配预期的类型“a->a->订购”
实际类型为“[t]”
在“sortList”的第一个参数中,即“[5,4,2,7,….””
在表达式中:sortList[5,4,2,7,…]
在‘it’的方程式中:it=sortList[5,4,2,…]
我的想法和尝试:

module Main where
import Data.List
sortList :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a]
sortList comparator list = sortBy comparator list
*Main> sortList [5,4,2,7,8,1]

<interactive>:1:10:
    Couldn't match expected type `a -> a -> Ordering'
                with actual type `[t]'
    In the first argument of `sortList', namely `[5, 4, 2, 7, ....]'
    In the expression: sortList [5, 4, 2, 7, ....]
    In an equation for `it': it = sortList [5, 4, 2, ....]

也许我调用的函数是错误的?我对哈斯克尔是个新手。我也尝试过多次搜索。我所能得出的结论是,在某些地方,这些类型并不匹配。我想对脚本的解释和指导会对我很有帮助。

您调用函数时出错,需要向它传递一个比较函数。只需向函数传递一个列表。

如果调用的函数错误,则需要向其传递一个比较器函数。您只需向函数传递一个列表。

您的函数签名显示:

“sortList是一个函数,它接受:”

  • 函数(a->a->Ordering),它接受两个类型为“a”的元素并返回一个“Ordering”[这是您的比较器]
  • 正在传递的项目“a”([a])列表
  • 返回项目“a”([a])的列表
试着这样称呼他们:

sortList compare [3,2,1]
欲了解更多信息,请阅读此处:

在这里:

您的函数签名显示:

“sortList是一个函数,它接受:”

  • 函数(a->a->Ordering),它接受两个类型为“a”的元素并返回一个“Ordering”[这是您的比较器]
  • 正在传递的项目“a”([a])列表
  • 返回项目“a”([a])的列表
试着这样称呼他们:

sortList compare [3,2,1]
欲了解更多信息,请阅读此处:

在这里:

你的签名已经说明了这一点:调用
sortList
需要一个
比较器
你没有提供……你的签名已经说明了这一点:调用
sortList
需要一个
比较器
你没有提供……你读过我的评论吗?您需要向函数传递一个比较器方法。”“比较”是您需要的比较器。更多阅读:这里:你读过我的评论吗?您需要向函数传递一个比较器方法。”“比较”是您需要的比较器。欲了解更多信息,请阅读以下内容: