压缩和排序问题
我在Scheme中找到了用于压缩的代码:
(define zip
(lambda (leftList rightList)
(if (null? rightList)
leftList
(if (member (car rightList) leftList)
(zip leftList (cdr rightList))
(zip (append leftList (list (car rightList))) (cdr rightList))))))
=> (zip '(1 4) '(2 3))
(1 4 2 3)
但我想对结果进行排序:
=> (zip '(1 4) '(2 3))
(1 2 3 4)
I found this code for zipping in Scheme:
(define zip
(lambda (leftList rightList)
(if (null? rightList)
leftList
(if (member (car rightList) leftList)
(zip leftList (cdr rightList))
(zip (append leftList (list (car rightList))) (cdr rightList))))))
=> (zip '(1 4) '(2 3))
(1 4 2 3)
But I want to sort the result:
=> (zip '(1 4) '(2 3))
(1 2 3 4)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的函数不叫
zip
;这称为合并
。既然这是一个家庭作业问题,提供解决方案是不负责任的。我只能提供这个:给定两个已排序的列表,将它们组合成已排序的新列表的最简单方法是什么?好吧,每个列表中的第一个元素(列表的
car
)是其自身列表中最小的元素,因此通过比较它们,您可以知道哪个是最小的两个< /em> 列出。然后,使用递归来合并剩余的内容。The function you are looking for is not called
zip
; it's calledmerge
. Since this is a homework problem, it would be irresponsible to provide the solution. I can only offer this:Given two lists that are already sorted, what's the easiest way to combine them into a new list that's sorted? Well, the first thing in each list (the
car
of the list) is the smallest element of its own list, so by comparing them, you can know which one is the smallest of both lists. Then, use recursion to merge what remains.