Triangular Recursion Function

Discussion of Common Lisp

Triangular Recursion Function

Postby MrGoat » Thu Dec 08, 2011 9:02 pm

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
MrGoat
 
Posts: 6
Joined: Mon Dec 05, 2011 8:39 pm

Re: Triangular Recursion Function

Postby smithzv » Thu Dec 08, 2011 9:55 pm

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.
smithzv
 
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am


Return to Common Lisp

Who is online

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

cron