Problem with my sine function

Discussion of Common Lisp

Problem with my sine function

Postby Owain » Thu Aug 23, 2012 11:19 am

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? :)
Owain
 
Posts: 7
Joined: Fri Aug 17, 2012 3:58 pm

Re: Problem with my sine function

Postby Goheeca » Thu Aug 23, 2012 12:06 pm

You have doubled parentheses around dotimes, further missing ones at second and fourth invocation of odd macro.
// I recommend you to use some IDE.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 197
Joined: Thu May 10, 2012 12:54 pm

Re: Problem with my sine function

Postby Owain » Thu Aug 23, 2012 12:44 pm

Okay, thank-you :) As it goes, I am using an IDE, the Lispbox project :)
Owain
 
Posts: 7
Joined: Fri Aug 17, 2012 3:58 pm

Re: Problem with my sine function

Postby Goheeca » Thu Aug 23, 2012 12:55 pm

OK. I think, the proper indentation (for example provided by Emacs) can help.
// I don't know, how Lispbox deals with that.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 197
Joined: Thu May 10, 2012 12:54 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests