A community for students.
Here's the question you clicked on:
 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.
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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0The 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 (mapiter inlist outlist) ; internal procedure to transform (if (empty? inlist) ; inlist to outlist outlist (mapiter (cdr inlist) ; tailrecursive (iterative) call to mapiter (append outlist ; with rest of the inlist and appended outlist (list (fn (car inlist))))))) (mapiter lst '())) ; just calls mapiter 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!

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Nice! 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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 setcdr! in the code.
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.