对列表进行排序的方案
好吧,我正在尝试获取一个列表并将其从最大到最小排序。
Example:
> (maxheap (list 5 6 2 1 18 7))
;output:
> (18 7 6 5 2 1)
到目前为止,这就是我得到的结果:
(define (mkmaxheap heaplist)
(let ((max (mymax(heaplist))))
;mymax is a func that returns max number, it works
(let (( head (car heaplist)) (tail (cdr heaplist)))
(if (null? tail)
newlist))))
这就是我能编译的所有内容,我编写的所有其他代码都失败了。任何有关解决此问题的帮助将不胜感激。
Okay so I am trying to take in a list and sort it from greatest to smallest.
Example:
> (maxheap (list 5 6 2 1 18 7))
;output:
> (18 7 6 5 2 1)
So here's what I got so far:
(define (mkmaxheap heaplist)
(let ((max (mymax(heaplist))))
;mymax is a func that returns max number, it works
(let (( head (car heaplist)) (tail (cdr heaplist)))
(if (null? tail)
newlist))))
Thats all I could get to compile, all the other code I wrote failed. Any help on solving this would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该仔细阐明要用于生成排序列表的策略。是这样的吗?
这不是一种非常快的排序方法,但它应该有效。代码的下一步将是编写一个函数来获取列表中除最大值之外的其余部分(如果列表有重复项,请注意正确处理它。)
一旦编写完成,您应该能够编写Scheme代码看起来或多或少类似于上面的轮廓。
You should articulate carefully the strategy that you want to use for producing the sorted list. Is it something like this?
This is not a very fast way to sort it, but it should work. The next step from your code will be to write a function to get the rest of the list except for the maximum (take care to handle it correctly if the list has duplicates.)
Once you have that written, you should be able to write Scheme code that looks more or less just like the outline above.
这是 Common Lisp 中的合并排序算法。它大致接近于在方案中实现相同的排序。
This is a merge sort algorithm in Common lisp. It's roughly close to implementing the same sort in scheme.
您需要决定使用什么来对列表进行排序。我最近一直在修改方案,通过 SICP 和“Schemer”系列,我发现在方案中实现冒泡排序、合并排序和快速排序非常容易。
You need to decide what to use to sort the list. I've been tinkering about with scheme recently, working my way through SICP and the "Schemer" series, and I found it pretty easy to implement a bubble sort, merge sort, and quicksort in scheme.
您没有指定您正在使用的实现。但它可以实现 r6rs list-排序或srfi-95排序或任何其他内置- 排序中。查看您的实施文档。
You didn't specify the implementation you're using. But it may implement either r6rs list-sort or srfi-95 sort or any other built-in sorting. Check out your implementation's documentation.