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)
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
expr1andexpr2?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:
Above function still has an error, which is easy to fix. You don’t want to execute the multiplication.