0

I'm trying to get this function to display the literal expr2 and expr1 as they are entered. The data that is entered is of the form (+ x y).

(DEFUN  deriv (expr var)    ;  function name and arguments
  (COND  ( (ATOM  expr)     ;  check for atomic expression (variable or constant)
              (IF  (EQL  expr var)
                  1     
                  0     
              ) )       
        ( (EQL  (FIRST  expr)  '+)  (deriv-add (SECOND expr) (THIRD expr) var) )
        ( (EQL  (FIRST  expr)  '-)  (deriv-minus (SECOND expr) (THIRD expr) var) )
        ( (EQL  (FIRST  expr)  '*)  (deriv-multi (SECOND expr) (THIRD expr) var) )
        ( (EQL  (FIRST  expr)  '/)  (deriv-divide (SECOND expr) (THIRD expr) var) )
        ( T  (ERROR  "UNKNOWN arithmetic operator"))
  )
)

(DEFUN  deriv-multi (expr1 expr2 var)
   (LIST '+ (* (deriv expr1 var) expr2) (* expr1 (deriv expr2 var)))
)

(SETQ e2 '(* (+ x y) (+ y 7)) )
(DERIV e2 'x)

1 Answer 1

3

This is a good basic introduction to Lisp:

http://www.cs.cmu.edu/~dst/LispBook/

'Common Lisp: A Gentle Introduction to Symbolic Computation' by David S. Touretzky.

Free PDF.

Btw., your function displays nothing. It calls the function 'LIST'. Why are there parentheses around expr1and expr2?

About your code:

  • it is good coding style in Lisp to use self-explaining code. Since you can use symbols to really name things (and not be abbreviations of concepts), you can use descriptive symbols. Entering longer symbols is then usually done by using symbol completion in the editor. This allows you to get rid of the comments.

  • don't add additional space. Write compact code.

  • format your code nice and compact.

  • use indentation and additional lines where it helps reading the code

Example:

(defun derive (expression variable)
  (if (atom expression)
      (if (eql expression variable) 1 0)
    (funcall (case (first expression)
               (+ #'derive-addition)
               (- #'derive-subtraction)
               (* #'derive-multiplication)
               (/ #'derive-division)
               (otherwise (error "unknown arithmetic operator")))
             (second expression) (third expression) variable)))

(defun derive-multiplication (expression1 expression2 variable)
  (list '+
        (* (derive expression1 variable)
           expression2)
        (* expression1
           (derive expression2 variable))))

Above function still has an error, which is easy to fix. You don't want to execute the multiplication.

(defun test ()
  (assert (equal (derive '(* (+ x y) (+ y 7)) 'x)
                 '(+ (* (+ 1 0)
                        (+ y 7))
                     (* (+ x y)
                        (+ 0 0)))))))
Sign up to request clarification or add additional context in comments.

8 Comments

I added more code to better demonstrate what it's trying to be used for.
@Brian: what for are the parentheses around expr1?
Sorry, that was a mistake. I removed them.
@Brian: you create a list with the + in front. But you execute the multiplication. Do you want to do that?
I added a test case, and this to my description. Running with (DERIV e2 'x) should result in the output (1 + 0) * (y + 7) + (x + y) * (0 + 0). Though in prefix notation. If that doesn't answer your question let me know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.