0

This is a procedure to return the last item in a list 1:

proc last (1)
if    (isEmpty(1))
       error('Oops. Empty list in procedure last.')
elseif (is empty(rest1))
       return (first(1)
else   return last(rest(1))

Modify that to create a recursive procedure getItem(i,l) that returns item i in a list 1, where i is an integer greater than zero.

proc getItem(i,1)
if    (isEmpty(1))
      error('Opps. Empty list in procedure last.')
else if (isEmpty(getItem,i1)
else if i > 0
      return item(i,1)

is that correct?

0

2 Answers 2

1

For it to work recursively, that last line needs to have the same name of the function.

Besides that, you need to decrease i .... otherwise you aren't moving...

should be something like:

proc getItem(i,1)
if    (isEmpty(1))
      error('Opps. Empty list in procedure last.')
else if (i > 0)
     return getItem(i-1,1)
else return first(1)
Sign up to request clarification or add additional context in comments.

Comments

0

What language are you using? In most languages you cannot use numbers as variable names. Also, you're missing a couple of parenthesis and you're not using 'else if' consistently.

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.