1

I want to verify if this a recursive function? Needs to return the nth element, mine works I just want to make sure.

(defun nth2(n lst)
       (let((count 1))
           (loop
               (cond((equal count n)(return (car lst))))
               (setq count (+ count 1))
               (setq lst (cdr lst)))))

Ok. I just tried this idea. It gives me an error: Not inside a block named NIL

(defun nth2(n lst)
    (let((count 1))
      (cond((equal count n)(return (car lst)))
          (t(setq count (+ count 1))
          (nth2(count (cdr (lst))))))))
2
  • 3
    No, it's not a recursive function. In general, recursive functions call themselves, so you should see nth2 inside the definition of nth2. Commented Nov 16, 2013 at 16:09
  • If I recall the function with something like (n-1) for example Commented Nov 16, 2013 at 16:12

2 Answers 2

2

As I wrote in this answer, you need to avoid setq, setf and the like with recursive functions.

Instead of counting up and having to use an additional variable, you can count down. Assuming indexing starts at 0, for example:

  (nth2 2 '(a b c d e))
= (nth2 1 '(b c d e))
= (nth2 0 '(c d e))
= (car '(c d e))
= 'c

so you have to recurse down to 0 while dropping the first element, then return the first element when the index is 0:

(defun nth2 (n lst)
  (if (zerop n)
    (car lst)
    (nth2 (1- n) (cdr lst))))
Sign up to request clarification or add additional context in comments.

4 Comments

ok, how should I proceed? Do I first try to work out the problem on paper and then code?
You need to find a good introduction to Scheme; I personally found books like The little Schemer extremely helpful. Working it out on paper is indeed a good idea.
BTW - you haven't yet accepted any answer for the questions you posted here on SO. If there was at least one correct answer, please accept the one you liked best.
Yes, I just saw were we are supposed to do this. I was searching.
1

Here's a general overview of how recursion works... Every recursive function must have:

  1. at least one base case: In base cases, the recursive function does something directly, without calling itself. A good recursive function has simple, easy-to-understand base cases. Recursive functions have base cases because the recursion has to stop somewhere.

  2. at least one recursive case: In recursive cases, the recursive function breaks a complex problem down into one or more easier problems to solve, and it does so by calling itself. The parameters used to call itself eventually need to take it closer to the base cases (e.g. using n-1 or (cdr my-list)) so that the recursion stops.

Your function doesn't have any recursive cases, so it's not a recursive function. In order to make it recursive, think about:

  1. What do I want my base case to be? What's an easy problem to solve? (Hint: It's easy to replace the value at the front of a list.)

  2. What do I want my recursive case to be? How can I make the problem look more like my base case?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.