(cond
;; (append (cons x y) z) => (cons x (append y z))
((and (pair? arg1)
(eq? (car arg1) 'cons))
(qq-cons (cadr arg1) (qq-append (caddr arg1) arg2)))
;; (append (list x) y) => (cons x y)
;; (append (list x . y) z) => (cons x (append (list . y) z))
((and (pair? arg1)
(eq? (car arg1) 'list))
(if (null? (cddr arg1))
(qq-cons (cadr arg1) arg2)
(qq-cons (cadr arg1)
(qq-append (cons 'list (cddr arg1))
arg2))))
;; (append x (append . y)) => (append x . y)
((and (pair? arg2)
(eq? (car arg2) 'append))
(cons 'append (cons arg1 (cdr arg2))))
;; (append x (quote ())) => x
((equal? arg2 '(quote ()))
arg1)
(else
(list 'append arg1 arg2))))