乐筑天下

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

[编程交流] Mapcar + lambda Description

[复制链接]

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2022-7-6 10:29:38 | 显示全部楼层 |阅读模式
Hi GUYS.
 
I have read the Help which attached with Autocad and Afralisp explainations about
mapcar and lambda but without a chance to understand.
 
So if someone could help me with it, It would be very kind favor.
 
Thankxxxxxxx
 
Sweety.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 10:34:42 | 显示全部楼层
Mapcar combines lists, or can take an action on a list - in either event it (mapcar) returns the result as a list.
 
Lambda is an anonymous function within itself, which does not require a defun.
 
Often times, mapcar and lambda are used together to yield a desired result... Apply and lambda can also be combined, but lambda can be used alone as well.
 
Do you have something specific you want to code, or was this just a general inquiry?
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2022-7-6 10:39:04 | 显示全部楼层
Thanks Renderman.
 
You are always in the first .
 
I have a code but it's not mine and do not want to post to avoid any screw up from anyone.
 
So in general like this one .... I hope it has written right .
 
  1. (mapcar '(lambda (x y) (+ (x y) 1.0) '(pt1 pt2)))
 
Question. how (x y) would be added to each other since they have no values .
 
If you add more explainations, It would be a very kind of you as usual.
 
Thankxxxxxxxxxxxxxxxxxxxxxxxxxx.
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2022-7-6 10:40:56 | 显示全部楼层
 
Thanks.
 
But it's not open for public .
 
I have no username in that Forum.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 10:45:23 | 显示全部楼层
The way I see it, here is the crux of it, mapcar evaluates a  function on every element of a list and returns a list containing the  result of each such evaluation. The function can be any function, taking  any number of arguments. lambda describes an anonymous function  (usually used for a one-off usage, hence not warranting the overhead of a  'defun'), and, as a function, can be supplied as an argument to mapcar.
 
Examples are the best way to learn:
 
  1. ;; Add 1 to every element of a list:(mapcar '1+ '(1 2 3 4 5))==>  '(2 3 4 5 6)
Note that the function argument is quoted (using the apostrophe), as it is to  be taken as an argument for mapcar and hence not evaluated. The  apostrophe means, in laymans terms: take this statement at face value and  don't evaluate it.
 
More Examples:
 
  1. (defun AddTwo ( x ) (+ x 2))(mapcar 'AddTwo '(1 2 3 4 5))==> (3 4 5 6 7)(mapcar '(lambda ( x ) (+ x 2)) '(1 2 3 4 5))==> (3 4 5 6 7)
  1. (mapcar '+ '(1 2 3 4 5) '(3 4 5 6 7))==> (4 6 8 10 12) = ((+ 1 3) (+ 2 4) (+ 3 5) (+ 4 6) (+ 5 7))
  1. (defun foo ( string number ) (strcat string (itoa number)))(mapcar 'foo '("a" "b" "c") '(1 2 3))("a1" "b2" "c3")(mapcar '(lambda ( string number ) (strcat string (itoa number))) '("a" "b" "c") '(1 2 3))("a1" "b2" "c3")
And using Apply:
 
  1. (defun averageof2 ( num1 num2 ) (/ (+ num1 num2) 2.))(apply 'averageof2 '( 1 2 ))==> 1.5(apply '(lambda ( num1 num2 ) (/ (+ num1 num2) 2.)) '( 1 2 ))==> 1.5
Or, for a generic average function perhaps:
 
  1. (defun average ( lst ) (/ (apply '+ lst) (float (length lst))))(average '( 1 2 3 4 ))==>  2.5
Or, here's one to mess with your head, average of a list of points:
 
  1. (defun AveragePoint ( l ) (mapcar '(lambda ( x ) (/ x (float (length l)))) (apply 'mapcar (cons '+ l))))(averagepoint '(( 2 3 1 ) ( 2 4 1 ) ( 1 5 1 )))==> (1.66667 4.0 1.0)
Lee
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 10:47:50 | 显示全部楼层
Great examples, Lee (as usual). :wink:
 
The only thing I would add, is that another major 'appeal' to using mapcar is that it can handle a list of any length.
 
Sometimes you might only have a handful of items in your list, other times it could be +1000... in either case, mapcar (along with apply) can handle the job.
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2022-7-6 10:50:21 | 显示全部楼层
Wow  
 
I was shocked with these examples, they are really very helpful.
 
So I have to clean up my mind to start studing these examples.
 
Thank you so much LEE.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 10:54:30 | 显示全部楼层
Happy to help out, if you have any questions about my examples, just ask
回复

使用道具 举报

55

主题

133

帖子

78

银币

后起之秀

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

铜币
280
发表于 2022-7-6 10:57:24 | 显示全部楼层
 
Dear Mr.Lee,
 
This is perfect examples and you are really great and it will helpful to everybody even if they know about mapcar.
 
Thank you once again for your great effort.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 11:00:07 | 显示全部楼层
 
Thanks Muthu for your kind words, I'm happy that many members may benefit from my post
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-6 17:52 , Processed in 0.546424 second(s), 72 queries .

© 2020-2025 乐筑天下

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