Just do a 1-to-1 translation. Assuming you want Common Lisp, here's the implementation:
(defun sq (n)
(if (= n 1)
1
(+ (sq (1- n)) n n -1)))
Question
I am looking for some help with a LISP function.
I have to write a LISP function that defines a recursive version of the function:
Sq(1) = 1
Sq(n) = sq(n-1)+2*n – 1 if n>1
If anyone can point me to a helpful website or able to provide an answer, that would be greatly appreciated!
Thanks.
Solution
Just do a 1-to-1 translation. Assuming you want Common Lisp, here's the implementation:
(defun sq (n)
(if (= n 1)
1
(+ (sq (1- n)) n n -1)))