Pergunta

I'm new to Lisp and I'm working through some tutorials. Below is the given code for differentiating polynomials. I want to streamline it so (d) and (simplify) are one function / can be done in one step. I was thinking something (defun differentiate (poly x) (simplify (d (poly x)))) but then it thinks poly is a function, which doesn't work.

Maybe this doesn't work because the input for (d) has to be quoted? i.e. (d '(+ x y) 'x)

Sorry for the large chunk of code but I thought it would be best to put it all in. The relevant functions are at the bottom.

;;
;; Constructors for polynomials
;;

(defun make-constant (num)
  num)

(defun make-variable (sym)
  sym)

(defun make-negation (poly)
  (list '- poly))

(defun make-sum (poly1 poly2)
  (list '+ poly1 poly2))

(defun make-difference (poly1 poly2)
  (list '- poly1 poly2))

(defun make-product (poly1 poly2)
  (list '* poly1 poly2))

(defun make-power (poly num)
  (list '** poly num))

;;
;; Recognizers for polynomials
;;

(defun constant-p (poly)
  (numberp poly))

(defun variable-p (poly)
  (symbolp poly))

(defun negation-p (poly)
  (and (listp poly) (eq (first poly) '-) (null (rest (rest poly)))))

(defun sum-p (poly)
  (and (listp poly) (eq (first poly) '+)))

(defun difference-p (poly)
  (and (listp poly) (eq (first poly) '-) (not (null (rest (rest poly))))))

(defun product-p (poly)
  (and (listp poly) (eq (first poly) '*)))

(defun power-p (poly)
  (and (listp poly) (eq (first poly) '**)))

;;
;; Selectors for polynomials
;;

(defun constant-numeric (const)
  const)

(defun variable-symbol (var)
  var)

(defun negation-arg (neg)
  (second neg))

(defun sum-arg1 (sum)
  (second sum))

(defun sum-arg2 (sum)
  (third sum))

(defun difference-arg1 (diff)
  (second diff))

(defun difference-arg2 (diff)
  (third diff))

(defun product-arg1 (prod)
  (second prod))

(defun product-arg2 (prod)
  (third prod))

(defun power-base (pow)
  (second pow))

(defun power-exponent (pow)
  (third pow))

;;
;; Unevaluated derivative
;;

(defun make-derivative (poly x)
    (list 'd poly x))

(defun derivative-p (poly)
  (and (listp poly) (eq (first poly) 'd)))

;;
;; Differentiation function
;;

(defun d (poly x)
  (cond
   ((constant-p poly) 0)
   ((variable-p poly) 
    (if (equal poly x) 
    1 
      (make-derivative poly x)))
      ((negation-p poly)
    (make-negation (d (negation-arg poly) x)))
   ((sum-p poly) 
    (make-sum (d (sum-arg1 poly) x) 
          (d (sum-arg2 poly) x)))
      ((difference-p poly)
    (make-difference (d (difference-arg1 poly) x)
             (d (difference-arg2 poly) x)))
   ((product-p poly) 
    (make-sum (make-product (product-arg1 poly) 
                (d (product-arg2 poly) x))
          (make-product (product-arg2 poly) 
                (d (product-arg1 poly) x))))
   ((power-p poly)
    (make-product (make-product (power-exponent poly)
                (make-power (power-base poly) 
                        (1- (power-exponent poly))))
          (d (power-base poly) x)))))

;;
;; Simplification function
;;

(defun simplify (poly)
  "Simplify polynomial POLY."
  (cond
   ((constant-p poly) poly)
   ((variable-p poly) poly)
      ((negation-p poly) 
    (let ((arg (simplify (negation-arg poly))))
      (make-simplified-negation arg)))
   ((sum-p poly)
    (let ((arg1 (simplify (sum-arg1 poly)))
      (arg2 (simplify (sum-arg2 poly))))
      (make-simplified-sum arg1 arg2)))
   ((product-p poly)
    (let ((arg1 (simplify (product-arg1 poly)))
      (arg2 (simplify (product-arg2 poly))))
      (make-simplified-product arg1 arg2)))
      ((difference-p poly)
    (let ((arg1 (simplify (difference-arg1 poly)))
      (arg2 (simplify (difference-arg2 poly))))
      (make-simplified-difference arg1 arg2)))
   ((power-p poly)
    (let ((base (simplify (power-base poly)))
      (exponent (simplify (power-exponent poly))))
      (make-simplified-power base exponent)))
   ((derivative-p poly) poly)))

(defun make-simplified-negation (arg)
  "Given simplified polynomial ARG, construct a simplified negation of ARG."
  (cond
   ((and (constant-p arg) (zerop arg)) arg)
   ((negation-p arg)                   (negation-arg arg))
   (t                                  (make-negation arg))))

(defun make-simplified-sum (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified sum of ARG1 and ARG2."
  (cond
   ((and (constant-p arg1) (zerop arg1)) arg2)
   ((and (constant-p arg2) (zerop arg2)) arg1)
      ((negation-p arg1)                   (make-simplified-difference 
                     arg2 (negation-arg arg1)))
      ((negation-p arg2)                   (make-simplified-difference 
                     arg1 (negation-arg arg2)))
   (t                                   (make-sum arg1 arg2))))

(defun make-simplified-difference (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified difference of ARG1 and ARG2."
  (cond
   ((and (constant-p arg2) (zerop arg2)) arg1)
   ((and (constant-p arg1) (zerop arg1)) (make-simplified-negation arg2))
   ((negation-p arg2)                    (make-simplified-sum 
                      arg1 (negation-arg arg2)))
   (t                                    (make-difference arg1 arg2))))

(defun make-simplified-product (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified product of ARG1 and ARG2."
  (cond
   ((and (constant-p arg1) (zerop arg1)) (make-constant 0))
   ((and (constant-p arg2) (zerop arg2)) (make-constant 0))
   ((and (constant-p arg1) (= arg1 1))   arg2)
   ((and (constant-p arg2) (= arg2 1))   arg1)
      ((and (constant-p arg1) (= arg1 -1))  (make-simplified-negation arg2))
      ((and (constant-p arg2) (= arg2 -1))  (make-simplified-negation arg1))
   (t                                    (make-product arg1 arg2))))

(defun make-simplified-power (base exponent)
  "Given simplified polynomials BASE and EXPONENT, construct a simplified power with base BASE and exponent EXPONENT."
  (cond
   ((and (constant-p exponent) (= exponent 1))   base)
   ((and (constant-p exponent) (zerop exponent)) (make-constant 1))
   (t                                            (make-power base exponent))))
Foi útil?

Solução

The inputs to poly don't "need to be quoted". Instead of

(d '(+ x y) 'x)

you could write any of the following:

(d (list '+ 'x 'y) 'x)

(d (make-sum 'x 'y) 'x)

(let ((ex 'x) (why 'y))
  (d (list '+ ex why) ex))

In your attempt in the question, you're trying to call d with the result of (poly x) which tries to call a function a named poly (actually, the function binding of the symbol poly, or some other possible things, but that's probably more in-depth than we need to get right now) with the value of the variable x:

(defun differentiate (poly x)
  ;; call poly with the value of x to produce a value y, and
  ;; then call d with y to produce a value z, and then call
  ;; simplify with z.
  (simplify (d (poly x))))

That won't work, of course, because there's no function named poly, and even if there was, d expects two arguments, not one. Instead, the first thing you should do is call d with two arguments, viz., the values of the variables poly and x, and then call simplify with the result of that:

(defun differentiate (poly x)
  (simplify (d poly x)))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top