I'm working on the derivative function in sicp and want to process expressions to simplify them. In order to handle addition with multiple terms, I want to convert expressions like '(+ a (+ b c)) into '(+ a b c). My make-sum function takes in an arbitrary number of terms, and only works if there are two or more terms. I'm trying to flatten the terms in the list when they are addition. (define (flatten lst sym) (cond ((eq? lst '()) '()) ((list? (car lst)) (cond ((eq? (car lst) sym) (append (flatten (car lst) sym) (flatten (cdr lst) sym))) (else (cons (car lst) (flatten (cdr lst) sym))))) (else (cons (car lst) (flatten (cdr lst) sym))))) (define (make-sum . ad) (cond ((> (length ad) 1) `(+ ,@(filter (lambda (x) (not (eq? x '+))) (filter (lambda (x) (not (eq? x 0))) (flatten ad '+))))) (else ad))) ;example (make-sum '(+ 1 2 3) 4 5) => (+ (+ 1 2 3) 4 5) ;;;This is wrong, should output (+ 1 2 3 4 5)