2015-02-10 99 views

回答

11

使用append本:

(append '(1 2 3 4) '(15 16)) 
=> '(1 2 3 4 15 16) 
4

或者,如果您正在合併兩個排序列表,並需要保持在結果列表中的排序:

(require srfi/1) 
(define (merge-sorted-lists lhs rhs #:order (lt? <)) 
    (let loop ((result '()) 
      (lhs lhs) 
      (rhs rhs)) 
    (cond ((null? lhs) (append-reverse result rhs)) 
      ((null? rhs) (append-reverse result lhs)) 
      ((lt? (car rhs) (car lhs)) 
      (loop (cons (car rhs) result) lhs (cdr rhs))) 
      (else 
      (loop (cons (car lhs) result) (cdr lhs) rhs))))) 

例子:

> (merge-sorted-lists '(1 3 5 7) '(2 4 6 8)) 
'(1 2 3 4 5 6 7 8) 
> (merge-sorted-lists '(7 5 3 1) '(8 6 4 2) #:order >) 
'(8 7 6 5 4 3 2 1) 
相關問題