2017-10-10 77 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