高阶函数
我无法理解我的讲师希望我从这个问题中做什么。谁能帮我解释一下他要我做什么?
定义插入排序算法的高阶版本。那就是定义 函数
insertBy :: Ord b => (a->b) -> a -> [a] -> [a]
inssortBy :: Ord b => (a->b) -> [a] -> [a]
,这一点让我感到困惑: 这样 inssort fl 对列表 l 进行排序,使得元素 x 位于元素 y 之前(如果 fx ) fy。
I am having trouble understanding what my lecturer want me to do from this question. Can anyone help explain to me what he wants me to do?
Define a higher order version of the insertion sort algorithm. That is define
functions
insertBy :: Ord b => (a->b) -> a -> [a] -> [a]
inssortBy :: Ord b => (a->b) -> [a] -> [a]
and this bit is where it got me confused:
such that inssort f l sorts the list l such that an element x comes before an elementyif f x < f y.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您对数字进行排序,那么 x < 就很清楚了。 y 的意思是。但如果你要对字母进行排序呢?还是客户?或者其他没有明确(对计算机)排序的东西?
因此,您应该创建一个函数 f() 来定义排序过程的顺序。 f() 将获取字母或客户或其他任何内容,并为计算机实际可以排序的每个字母返回一个整数。
至少,问题是这样描述的。我个人会设计一个谓词,它接受两个项目 x 和 y,并在 x < 时返回一个布尔值。 y。但无论哪个都好。
If you were sorting numbers, then it's clear what x < y means. But what if you were sorting letters? Or customers? Or anything else without a clear (to the computer) ordering?
So you are supposed to create a function f() that defines that ordering for the sorting procedure. That f() will take the letters or customers or whatever and will return an integer for each one that the computer can actually sort on.
At least, that's how the problem is described. I personally would have designed a predicate that accepted two items, x and y and returned a boolean if x < y. But whichever is fine.
该代码希望您重写插入排序算法,但使用函数作为参数 - 因此是一个高阶函数。
我想指出的是,这段代码(包括拼写错误)似乎源于某所大学当前到期的一项工作 - 我在搜索“插入排序算法”时发现了此页面,因为我将术语复制粘贴到了文档也是如此,包括拼写错误。
从互联网上寻找代码是一件有风险的事情。我可以推荐插入排序算法维基百科条目,或者您的讲座幻灯片中提供的 Haskell 代码(您正在寻找“插入排序算法”和“高阶函数”),而不是您在 Stack 上放置的几个查询溢出?
The code wants you to rewrite the insertion sort algorithm, but using a function as a parameter - thus a higher order function.
I would like to point out that this code, typo included, seems to stem from a piece of work currently due at a certain university - I found this page while searching for "insertion sort algortihm", as I copy pasted the term out of the document as well, typo included.
Seeking code from the internet is a risky business. Might I recommend the insertion sort algorithm wikipedia entry, or the Haskell code provided in your lecture slides (you are looking for the 'insertion sort algorithm' and for 'higher-order functions), as opposed to the several queries you have placed on Stack Overflow?