Page 1 of 1

Problem with my sine function

Posted: Thu Aug 23, 2012 11:19 am
by Owain
Hello, I have been learning Common Lisp only for a few days. I thought I would try to write my own simple program to calculate the sine of a number by going though the Taylor series.
Here is my program:

Code: Select all

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

(defmacro odd (n)
  `(1+ (* ,n 2)))

(defun sine (n &optional (a 3)) ;n is the number, a is the accuracy
  (let ((result n))
    ((dotimes (i a)
       (if (mod (1+ i) 2)
	   (setf result (- result (/ (expt n (odd (1+ i))) (fact (odd 1+ i)))))
	   (setf result (+ result (/ (expt n (odd (1+ i))) (fact (odd 1+ i))))))))))
When I try to compile the sine function I get the error:

Code: Select all

While compiling SINE :
In the form (#1=(DOTIMES (I A)
                  (IF (MOD # 2)
                      (SETF RESULT #)
                      (SETF RESULT
                            #)))), #1# is not a symbol or lambda expression.
   [Condition of type CCL::COMPILE-TIME-PROGRAM-ERROR]
Does anyone know what the problem is? :)

Re: Problem with my sine function

Posted: Thu Aug 23, 2012 12:06 pm
by Goheeca
You have doubled parentheses around dotimes, further missing ones at second and fourth invocation of odd macro.
// I recommend you to use some IDE.

Re: Problem with my sine function

Posted: Thu Aug 23, 2012 12:44 pm
by Owain
Okay, thank-you :) As it goes, I am using an IDE, the Lispbox project :)

Re: Problem with my sine function

Posted: Thu Aug 23, 2012 12:55 pm
by Goheeca
OK. I think, the proper indentation (for example provided by Emacs) can help.
// I don't know, how Lispbox deals with that.