Sign in to follow this  
red-dragonX

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

Recommended Posts

red-dragonX    122
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
Roboguy    794

(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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this