muthu123 Posted April 20, 2010 Posted April 20, 2010 What is the difference between the function 'foreach and 'mapcar with lambda? Can you explain with clear example? Quote
MSasu Posted April 20, 2010 Posted April 20, 2010 Please check this tutorial: http://www.afralisp.net/autolisp/tutorials/mapcar-and-lambda.php. Regards, Quote
muthu123 Posted April 20, 2010 Author Posted April 20, 2010 Please check this tutorial: http://www.afralisp.net/autolisp/tutorials/mapcar-and-lambda.php. Regards, Thank you so much. Now i can understan mapcar, Lambda and apply. But still i need some more examples for the function 'foreach. Quote
MSasu Posted April 20, 2010 Posted April 20, 2010 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, Quote
Lee Mac Posted April 20, 2010 Posted April 20, 2010 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 Quote
MSasu Posted April 20, 2010 Posted April 20, 2010 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, Quote
Freerefill Posted April 20, 2010 Posted April 20, 2010 Lee has a habit of being awesome 24/7. It's a little hard for newbies to comprehend. Quote
Lee Mac Posted April 20, 2010 Posted April 20, 2010 Lee has a habit of being awesome 24/7. It's a little hard for newbies to comprehend. Haha you flatter me too much Quote
ketxu Posted May 30, 2011 Posted May 30, 2011 (edited) 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 May 30, 2011 by ketxu Quote
irneb Posted May 30, 2011 Posted May 30, 2011 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 1 Quote
Recommended Posts
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.