• Advertisement
Sign in to follow this  

difference between map and accumulate procedures (Scheme help needed!)

This topic is 4367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm trying to understand the difference between accumulate and map. Here are the definitions: (define (map proc al) (cond ((null? al) ()) (else (cons (proc (car al)) (map proc (cdr al)))))) (define (accum proc init al) (cond ((null? al) init) (else (proc (car al) (accum proc init (cdr al)))))) Obvious difference: map: takes two args; ending result is a list. accum.: takes three args; ending result is not a list? (probably a number). Not sure if this makes sense.. but if there's a procedure that wants the end result to be a list, then map makes sense to use, right? Just trying to get it right; if any questions, please ask. Any help's appreciated, so TIA.

Share this post


Link to post
Share on other sites
Advertisement

(map proc (list a b c d e))
means

(list (proc a) (proc b) (proc c) (proc d) (proc e))
while

(accum proc init (list a b c d e))
means

(proc a (proc b (proc c (proc d (proc e init)))))


It's worth noting that accum is more general than map. Map can be implemented in terms of accum.

Also accum is sometimes known as "fold" or "foldr" (for "fold right").

EDIT: Also, for your final question, map is only useful if you want a one-to-one mapping between the input and output lists. If you want something else, you'll probably either have to use accum (if possible) or implement it yourself.

[Edited by - Roboguy on March 12, 2006 11:36:46 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement