Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute LISP Online

;Question 6
(defun expo (base exp)
    ( if (eq exp 0) 
        1 
        (* base (expo base (- exp 1))) 
    )
)

(defun fact (n)
    (if (= n 0)
      1
      (* n (fact (- n 1))) 
    ) 
)

(defun sin-cos-comp  (x n)
    (if (or (not(typep n 'integer)) (not (typep x 'integer)))
        (return-from sin-cos-comp "Invalid input")
    )	

    (if (oddp n) 
        (if (or(<= x -10)(>= x 10))
            (print "The value entered for x is not valid")
        )
    )    

    (setf counter 0)
    (if (evenp n)
        (setf result 1)
        (setf result x)
    )

    (if (evenp n)
            (loop for i from 1 to (/ n 2)
                do 
                    (if (evenp counter)
                        (setf result (- result (/ (expo x (* i 2)) (fact (* i 2))) ))
                        (setf result (+ result (/ (expo x (* i 2)) (fact (* i 2))) ))
                    )
                (setf counter (+ counter 1))
            )
            (loop for i from 1 to (/ (- n 1) 2)
                do 
                    (if (evenp counter)
                        (setf result (- result (/ (expo x (+ (* i 2) 1)) (fact (+(* i 2) 1)))))
                        (setf result (+ result (/ (expo x (+ (* i 2) 1)) (fact (+(* i 2) 1)))))
                    )
                (setf counter (+ counter 1))
            ) 
    )
    (block nil (return result))
)

(print (sin-cos-comp 8 2))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.