Difference between 'forea
What is the difference between the function 'foreach and 'mapcar with lambda?Can you explain with clear example? 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. 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, 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 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, Fair point - yours is a better example. Lee has a habit of being awesome 24/7. It's a little hard for newbies to comprehend.
Haha you flatter me too much 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 ^^
页:
[1]
2