muthu123 发表于 2022-7-6 09:24:16

Difference between 'forea

What is the difference between the function 'foreach and 'mapcar with lambda?
 
Can you explain with clear example?

MSasu 发表于 2022-7-6 09:33:54

Please check this tutorial: http://www.afralisp.net/autolisp/tutorials/mapcar-and-lambda.php.
 
Regards,

muthu123 发表于 2022-7-6 09:39:14

 
Thank you so much.
 
Now i can understan mapcar, Lambda and apply.
 
But still i need some more examples for the function 'foreach.

MSasu 发表于 2022-7-6 09:41:28

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,

Lee Mac 发表于 2022-7-6 09:46:15

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

MSasu 发表于 2022-7-6 09:52:54

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,

Lee Mac 发表于 2022-7-6 09:58:43

Fair point - yours is a better example.

Freerefill 发表于 2022-7-6 10:04:02

Lee has a habit of being awesome 24/7. It's a little hard for newbies to comprehend.

Lee Mac 发表于 2022-7-6 10:10:34

 
Haha you flatter me too much

ketxu 发表于 2022-7-6 10:14:36

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
查看完整版本: Difference between 'forea