## Problem with my sine function

Discussion of Common Lisp

### Problem with my sine function

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

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). Temporary mirrors of aferomentioned: CLHS and a dark version.

Goheeca

Posts: 271
Joined: Thu May 10, 2012 12:54 pm

### Re: Problem with my sine function

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

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). Temporary mirrors of aferomentioned: CLHS and a dark version.

Goheeca

Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Return to Common Lisp

### Who is online

Users browsing this forum: Bing [Bot] and 3 guests