如何对方案中的数字无序列表进行排序 [英] How to sort disorder list of numbers in scheme
本文介绍了如何对方案中的数字无序列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用Scheme 中的值对列表进行排序的正确方法是什么?例如,我有未排序的值:
What it the proper way to sort a list with values in Scheme? For example I have the values which are not ordered:
x1, x5, x32 .... xn
或
3, 4, 1, 3, 4, .. 9
首先我想为它们增加数字并按以下顺序显示它们:
First I want to for them by increase number and display them in this order:
x1, xn, x2, xn-1
或
1, 6, 2, 5, 3, 4
任何帮助都是有价值的.
Any help will be valuable.
推荐答案
这与您发布的问题相同 之前,但稍有改动.正如我在答案的评论中告诉您的那样,您只需在重新排列列表之前对其进行排序.这是一个球拍解决方案:
This is the same question you posted before, but with a small twist. As I told you in the comments of my answer, you just have to sort the list before rearranging it. Here's a Racket solution:
(define (interleave l1 l2)
(cond ((empty? l1) l2)
((empty? l2) l1)
(else (cons (first l1)
(interleave l2 (rest l1))))))
(define (zippy lst)
(let-values (((head tail) (split-at
(sort lst <) ; this is the new part
(quotient (length lst) 2))))
(interleave head (reverse tail))))
它按预期工作:
(zippy '(4 2 6 3 5 1))
=> '(1 6 2 5 3 4)
这篇关于如何对方案中的数字无序列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文