0

need help on making these two recursive programs in scheme iterative? I made the recursion, but am stuck at creating an iteration for both.

question 1 - recursion

(define mylength
 (lambda (l)
   (cond
   ((null? l) 0)
     (else (+ 1 (mylength (cdr l)))))))

question 1 - iteration?

question 2 - recursion

(define mylistref
  (lambda (l index)
   (cond
   ((= index 0)(car l))
     (else
       (mylistref (cdr l) (- index 1))))))

question 2 - iteration?

2
  • You probably mean recursive vs iterative process since Scheme only has recursion to do loops. Iterative calls itself in tail position thus the resutl fo the recursion is the result of the whote thing while recursive process needs to do something with the result. Thus in your first example it needs to add afterwards and thus recursive. Your second one is a iterative process since no work is done afterwards. Do you see the difference? Commented Oct 26, 2016 at 13:58
  • Possible duplicate of Recursion to iteration with proof? Commented Oct 26, 2016 at 15:45

1 Answer 1

1

Scheme does not have any looping structures so your only option is to use recursion if you are traversing over some kind of data structure. You can read more about it here

Sign up to request clarification or add additional context in comments.

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.