仅按方案按降序保留数字 [英] Only keep numbers in descending order by scheme
问题描述
我想写一个函数,只保留降序数字并去掉升序数字.
I want to write a function that just keeps the descending order numbers and gets rid of ascending ones.
例如:(降序 '(6 5 3 1 2 8))
应该给我(6 5 3 1)
.
谢谢.
推荐答案
列表是将对象添加到列表中的结果.或者一个空列表.
A list is the result of consing an object onto a list. Or an empty list.
什么是康辛?这是一个内置操作.
What is consing? It is a built-in operation.
(define (plain-cons x xs)
(cond
((null? xs) (list x))
(else (cons x xs)))) ; using the built-in
降序列表是将对象descend-consing 到降序 列表的结果.或者一个空列表.
A descending list is the result of descend-consing an object onto a descending list. Or an empty list.
什么是下降-consing?结果列表也是降序的:
What is a descend-consing? It is a consing such that the resulting list is also descending:
; (descend-cons 3 '()) -> (list 3)
; (descend-cons 8 '(7 3)) -> (cons 8 '(7 3))
; (descend-cons 5 '(8 7 3)) -> (descend-cons 5 '(7 3))
(define (descend-cons x xs)
(cond
((null? xs) (list x))
(else
(let ((a (car xs)))
(cond
((>= x a) ; { 8 '(7 3)) } -> '(8 7 3)
.... )
(else ; { 5 '(8 7 3)) } -> { 5 '(7 3) }
(.... x
(cdr xs))))))))
有了这个,任务就很容易了.我们将编写函数 descending
将列表转换为降序列表,就像
Armed with this, the task is easy. We'll write the function descending
which turns a list into a descending list, simply as
; (descending '()) -> '()
; (descending '(x y z ...)) -> (descend-cons x (..... '(y z ...)))
(define (descending lst)
(cond
((null? lst) lst)
(else
(let ((x (car lst))
(xs (cdr lst)))
(...... x
(...... xs))))))
descend-cons
期望的第二个参数是什么?它必须是一个降序列表.
What is the second argument expected by descend-cons
? It must be a descending list.
我们可以从列表'(y z ...)
中创建一个降序列表吗?我们的武器库中有什么功能可以为我们做到这一点?
Can we create a descending list, from the list '(y z ...)
? What function, that we have in our arsenal, would do this for us?
这篇关于仅按方案按降序保留数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!