2017-10-10 75 views
1

我想编写一个只保留降序数字并排除上升数字的函数。只按照计划降序排列数字

例如: (descending '(6 5 3 1 2 8)) 应该给我(6 5 3 1)

谢谢。

+0

什么是'notAsc'?你的意思是“降序”? – molbdnilo

+0

你确定你正在调用该功能吗?你在打撇号时是否有一个错字? – tmwoods

+0

我修改了函数以降序 – flower

回答

1

列表是将对象包含在列表中的结果。或者是一个空的列表。

什么是收购?这是一个内置操作。

(define (plain-cons x xs) 
    (cond 
    ((null? xs) (list x)) 
    (else (cons x xs)))) ; using the built-in 

降序列表是下降 -consing对象到降序列表的结果。或者是一个空的列表。

什么是下降 -consing?它是这样的结果列表也降一consing:

; (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 '())   -> '() 
; (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预期的第二个参数的函数descending?它必须是下降列表。

可以我们从列表中创建一个降序列表'(y z ...)?我们在武器库中有什么功能可以为我们做到这一点?

+0

很好的解决方案,但是为什么'cond'只有两个术语,并且不需要至少一个'begin'? – Sylwester

+2

@Sylwester我不知道,因为统一可能?我在这里首先关注正确性。即,*解决问题的方法。 –

+0

你知道我的程序有什么问题吗? – flower