乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 89|回复: 11

[编程交流] Difference between 'forea

[复制链接]

55

主题

133

帖子

78

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
280
发表于 2022-7-6 09:24:16 | 显示全部楼层 |阅读模式
What is the difference between the function 'foreach and 'mapcar with lambda?
 
Can you explain with clear example?
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 09:33:54 | 显示全部楼层
Please check this tutorial: http://www.afralisp.net/autolisp/tutorials/mapcar-and-lambda.php.
 
Regards,
回复

使用道具 举报

55

主题

133

帖子

78

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
280
发表于 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.
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 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,
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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:
 
  1. (foreach item '(0 1 2 3 4 5) (setq lst (cons (1+ item) lst)))
 
Will return:
 
  1. (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
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 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?
 
  1. (foreach item '(0 1 2 3 4 5)(setq lst (append lst (list (1+ item))))(print (1+ item)))
 
Regards,
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:58:43 | 显示全部楼层
Fair point - yours is a better example.
回复

使用道具 举报

20

主题

344

帖子

325

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 10:04:02 | 显示全部楼层
Lee has a habit of being awesome 24/7. It's a little hard for newbies to comprehend.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 10:10:34 | 显示全部楼层
 
Haha you flatter me too much
回复

使用道具 举报

22

主题

326

帖子

185

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
243
发表于 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 ^^
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-7 02:55 , Processed in 1.125922 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表