Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active August 29, 2015 14:04
Show Gist options
  • Save topher6345/f97b6ff6fba1965f25e7 to your computer and use it in GitHub Desktop.
Save topher6345/f97b6ff6fba1965f25e7 to your computer and use it in GitHub Desktop.
Lisp - Derivative

Homoiconicity

http://en.wikipedia.org/wiki/Homoiconicity

(setq y '(+ (* 7 (expt x 3) (* x 9) 2 (cos (* 3 x)))))

Predicates - test if something is true.

(defun deriv (expression x)
      (cond ((numberp expression) 0)
            ((equal expression) 1)
            ((symbolp expression) 0)
            ((t (let ((op (first expression)
                      (u (second expression)
                      (v (third expression ))))))))
                 (cond ((equal op '+ ) (list '+ (deriv u x) (deriv v x)))
                       ((equal op '* ) (list '+ u (deriv v x) )
                                       (list '+ v (deriv u x) ))
                       ((equal op 'expt) (list '* v (expt expression (- v 1))))
                       ((equal op 'cos) (list 'sin (deriv u x))))))

Are you hungry?

(foodp x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment