A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

The video lecture 3a states you can write map using an iterative process. I don't see how that is possible though. It seems like cons can only add something to the front of a list.

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The usual recursive map would be something like this: (define (map fn lst) (if (empty? lst) '() (cons (fn (car lst)) (map fn (cdr lst))))) To make map an iterative process, you could use append to add elements to the back: (define (map fn lst) (define (map-iter inlist outlist) ; internal procedure to transform (if (empty? inlist) ; inlist to outlist outlist (map-iter (cdr inlist) ; tail-recursive (iterative) call to map-iter (append outlist ; with rest of the inlist and appended outlist (list (fn (car inlist))))))) (map-iter lst '())) ; just calls map-iter with empty outlist Append joins two lists, so (list elem) is needed here. Test: (map sqrt '(1 4 9 16)) ; --> '(1 2 3 4) Cheers!

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Nice! That makes map itself iterative, but I think that just pushes the problem into append. Wouldn't append have to be recursive (and not iterative) to accomplish this?

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I just looked up the source to append in mit scheme. http://www.koders.com/scheme/fid0D80120BE6EEE75E849C32398E6154066A790CC3.aspx?s=md5#L512 The comments above say that it uses a finite amount of stack space. The code is a bit complex. It seems to me like it is able to do this by mutating the list. There is a reference to set-cdr! in the code.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.