Jump to content

Difference between 'foreach and 'mapcar


muthu123

Recommended Posts

You welcome! That tutorial contains a FOREACH example also.

 

For more FOREACH function's examples, I'm sure that you can find a lot by simply searching the forum.

 

Regards,

Link to comment
Share on other sites

The foreach loop uses a symbol to represent each element in the provided list argument, and evaluates the provided expressions within the foreach loop on every element in the list, for example:

 

(foreach item '(0 1 2 3 4 5)
 (setq lst (cons (1+ item) lst))
)

 

Will return:

 

(6 5 4 3 2 1)

 

Each element in the list is bound to the symbol 'item' and then the expressions are evaluated using this variable.

 

Hope this helps,

 

Lee

Link to comment
Share on other sites

No offence, but don’t you belive that using CONS to show the result may be confusing for a beginner? I mean the result list is reverted and have to figure out why from just adding 1 to each item? How about using LIST or PRINT?

 

(foreach item '(0 1 2 3 4 5)
(setq lst (append lst (list (1+ item))))
(print (1+ item))
)

 

Regards,

Link to comment
Share on other sites

  • 1 year later...

And please tell me what order do function item in list in Foreach ? The first to the Last or Last to First ?

Ex : (foreach x '( 1 2 3 4 5) (1+ x)...) : the order is (1+ 1) (1+ 2) (1+ 3)... or (1+ 5) (1+ 4) (1+ 3)...

( Srr, my English not well :( )

 

P/S 2 : Srr, i'd tested and found that foreach start from the first to last element ^^

Edited by ketxu
Link to comment
Share on other sites

And please tell me what order do function item in list in Foreach ? The first to the Last or Last to First ?

Ex : (foreach x '( 1 2 3 4 5) (1+ x)...) : the order is (1+ 1) (1+ 2) (1+ 3)... or (1+ 5) (1+ 4) (1+ 3)...

( Srr, my English not well :( )

 

P/S 2 : Srr, i'd tested and found that foreach start from the first to last element ^^

Both mapcar and foreach run from 1st to last. The major difference between the 2 is what's known in programming parlance as functional and procedural. In both the list is processes one item at a time starting from the 1st. The differences are:

 

  • With mapcar a function is applied to each item in turn, and then a new list containing the modified results is what comes out.
  • With foreach multiple statements are run with the current item in its nth state. No new list is (necessarily) returned or created - normally only the very last calculation is returned at the end of foreach.

Think of it as such:

 

  • When you want to modify each item in a list and get a resulting list - mapcar is most probably your choice.
  • If you want to use the list to modify something else foreach is probably best.

This does not mean that you use foreach when making a procedural (also called imperative) and mapcar when you're making a functional thing. You can easily alter the result and set variables states in either one. Thus what you do inside each can have similar effects, but on their own they're quite distinct.

 

E.g. let's say you've got a list of integers want to add 10 to each:

(mapcar '(lambda (item) (+ item 10)) '(1 2 3 4 5)) ;Result is (11 12 13 14 15)

To get the same using foreach:

(setq result nil)
(foreach item '(1 2 3 4 5)
 (setq result (cons (+ item 10) result)
) ;Last call to setq returns (15 14 13 12 11) because the cons adds the new item in front
(reverse result) ;So reverse the list to get (11 12 13 14 15)

Now let's say we want to sum each value together to get a grand total:

(setq total 0)
(mapcar '(lambda (item) (setq total (+ total item))) '(1 2 3 4 5))
;; Result of mapcar would be (1 3 6 10 15)
(princ total) ;Total's value becomes 15

Here it's probably more in-line with using foreach:

(setq total 0)
(foreach item '(1 2 3 4 5)
 (setq total (+ total item))
) ;The result at the end is 15, as is the value inside total.

Or slightly off topic: to use a pure functional way:

(apply '+ '(1 2 3 4 5)) ;Result is 15

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...