Unpack a list to use as args in func added by phasers_to_stun on Thu Jan 21 00:32:39 2016

so I have the following code for taking derivatives, and I have been modifying addition so I can use expression like (+ 1 2 (* 2 x)) instead of (+ 1 (+ 2 (* 2 x))). In this code from the derive function:

((sum? exp)
	 (eval `(make-sum ,@(map (lambda (t)
				 (deriv t var))
			       (cdr exp)))))

I use eval and a quasiquote to supply the items from the mapping as arguments to make-sum since it takes any variable number of arguments.
The problem is that non numeric terms must be supplied as symbols
i.e.: (** x 2) must be supplied as '(** x 2). I cant figure out how to supply non numeric terms as quoted items.

(use srfi-1)
(define (deriv exp var)
  (cond ((number? exp) 0)
	((variable? exp)
	 (if (same-variable? exp var) 1 0))
	((sum? exp)
	 (eval `(make-sum ,@(map (lambda (t)
				 (deriv t var))
			       (cdr exp)))))
	((product? exp)
	 (make-sum
	  (make-product (multiplier exp)
			(deriv (multiplicand exp) var))
	  (make-product (deriv (multiplier exp) var)
			(multiplicand exp))))
	((exponentiation? exp)
	 (make-product (exponent exp)
		       (make-product
			(make-exponentiation
			 (base exp)
			 (- (exponent exp) 1))
			(deriv (base exp) var))))
	((trig? exp)
	 (cond ((sin? exp)
		(make-product (make-trig 'cos
					 (trig-subexp exp))
			      (deriv (trig-subexp exp) var)))
	       ((cos? exp)
		(make-product -1
			      (make-product (make-trig 'sin
						       (trig-subexp exp))
					    (deriv (trig-subexp exp) var))))))
	(else
	 (error "unknown expression type -- DERIV" exp))))



(define (variable? x) (symbol? x))
(define (same-variable? v1 v2)
  (and (variable? v1) (variable? v2) (eq? v1 v2)))

(define (accumulate-terms lst)
  (define (accumulate-terms-help lst num-sum other-terms)
    (cond ((null? lst) `(,@other-terms ,num-sum))
	  ((number? (car lst)) (accumulate-terms-help
				(cdr lst)
				(+ num-sum (car lst))
				other-terms))
	  (else (accumulate-terms-help (cdr lst)
				       num-sum
				       (cons (car lst) other-terms)))))
  (accumulate-terms-help lst 0 '()))

(define (flatten lst sym)
   (cond ((eq? lst '()) '())
         ((list? (car lst))
	  (cond ((eq? (caar 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)
	  `(+ ,@(accumulate-terms
		 (filter (lambda (x)
			   (not (eq? x '+)))
			 (filter (lambda (x) (not (eq? x 0)))
				 (flatten ad '+))))))
	 (else ad)))

	  
(define (make-product m1 m2)
  (cond ((or (equal? m1 0)
	     (equal? m2 0))
	 0)
	((equal? m1 1) m2)
	((equal? m2 1) m1)
	((eq? m1 m2) (make-exponent m1 2))
	((and (number? m1) (number? m2)) (* m1 m2))
	((and (number? m1) (product? m2) (number? (multiplier m2)))
	 (list '* (* m1 (multiplier m2)) (multiplicand m2)))
	(else (list '* m1 m2))))
(define (sum? x)
  (and (pair? x) (eq? (car x) '+)))
(define (addend s) (cadr s))
(define (augend s) (cddr s))
(define (product? x)
  (and (pair? x) (eq? (car x) '*)))
(define (multiplier p) (cadr p))
(define (multiplicand p) (caddr p))
(define (exponentiation? x)
  (and (pair? x) (eq? (car x) '**)))
(define (base exp)
  (cadr exp))
(define (exponent exp)
  (caddr exp))
(define (make-exponentiation
	 base expo)
  (cond ((equal? expo 0) 1)
	((equal? expo 1) base)
	(else (list '** base expo))))

(define (trig? exp)
  (cond ((or (eq? 'sin (car exp))
	     (eq? 'cos (car exp))
	     (eq? 'tan (car exp)))
	 #t)
	(else #f)))

(define (make-trig fun subexp)
  (list fun subexp))

(define (sin? exp)
  (eq? (car exp) 'sin))
(define (cos? exp)
  (eq? (car exp) 'cos))
(define (trig-subexp exp)
  (cadr exp))

(define (symbol-replace exp sym rep)
  (map (lambda (s)		
	 (cond ((eq? s sym) rep)
	       ((pair? s) (symbol-replace s sym rep))
	       (else s)))
       exp))

(define (** base exp)
  (cond ((eq? exp 0) 1)
	((eq? exp 1) base)
	(else (* base (** base (- exp 1))))))



(display (deriv '(+ 2 3 (** x 2)) 'x))
(newline)
(display (addend (make-sum 2 3 '(** x 2))))
(newline)
(display (deriv '(** x 2) 'x))
(newline)
(display (deriv '(+ (** x 2) 3 2) 'x))