Page 1 of 1

Triangular Recursion Function

Posted: Thu Dec 08, 2011 9:02 pm
by MrGoat
Hi,

I am trying to solve this question :

Exercise: The N'th triangular number is defined to be 1 + 2 + 3 + ... + N. Alternatively, we could give a recursive definition of triangular number as follows:

T(n) = 1 if n = 1
T(n) = n + T(n-1) if n > 1

Use the recursive definition to help you implement a linearly recursive function (triangular N) that returns the N'th triangular number. Enter your function definition into a text file. Then load it into LISP. Evaluate (triangular 6).

Below is my code :

(defun triangle (N)
"Compute the Triangular Number of N."
(if (= N 1)
1
(+ N (triangular (- N 1)))))


When I loaded it into the Debug Window using (load "CallTriangle") , it says Loading C:\acl82express\CallTriangle.cl. How do I evaluate (triangular 6) ?


Thanks

Re: Triangular Recursion Function

Posted: Thu Dec 08, 2011 9:55 pm
by smithzv
I feel like you have some serious misconceptions of how you are supposed to be interacting with your Lisp. What do you mean "debug window"? I gotta tell you that I am pretty puzzled right now. I suppose your question...
MrGoat wrote:How do I evaluate (triangular 6) ?
...is most simply answered by, type the text "(triangular 6)" and then press the enter key. I assume that you mistakenly defined your function as "triangle" instead of "triangular"? Check your source again.