function - change - to + in Common Lisp -
is there way alter -
(minus) function +
(plus) function?
my homework implement sin calculation on macluaurin series
sin(x) = x-(x^3/3!)+(x^5/5!) -(x^7/7!)+(x^9/9!)-...
each article has different sign. lisp code
(defun sinmac (x series n plusminus) (cond ((= series 0) 0) (t (funcall plusminus (/ (power x n) (factorial n)) (sinmac x (- series 1) (+ n 2) plusminus)))))
is possible alter plusminus
exchange sign? if '+
function send '-
next recursive call. phone call (got '-
) phone call '+
, on...
you circular list. so:
(defun sin-mac (x series n plus-minus) (cond ((zerop series) 0) (t (funcall (car plus-minus) (/ (power x n) (factorial n)) (sin-mac x (1- series) (+ n 2) (cdr plus-minus)))))) (sin-mac x series 1 '#0=(+ - . #0#))
or better, wrap initial arguments using labels
:
(defun sin-mac (x series) (labels ((recur (series n plus-minus) (cond ((zerop series) 0) (t (funcall (car plus-minus) (/ (power x n) (factorial n)) (recur (1- series) (+ n 2) (cdr plus-minus))))))) (recur series 1 '#0=(+ - . #0#))))
function lisp common-lisp
No comments:
Post a Comment