乐筑天下

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

[编程交流] convert list with mapcar, lamb

[复制链接]

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-6 07:29:12 | 显示全部楼层 |阅读模式
I need to convert a list:
(51 41 83 10 60 32 46 84)
 
to list:
((51 41)(83 10)(60 32)(46 84))
 
Regards
回复

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-6 07:35:23 | 显示全部楼层
  1. (defun every2nd ( lst ) (vl-remove nil (mapcar '(lambda ( a ) (if (eq (rem (vl-position a lst) 2) 0) a)) lst)))(defun groupby2 ( lst ) (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst))))(groupby2 '(51 41 83 10 60 32 46 84))
 
M.R.
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 07:37:10 | 显示全部楼层
Maybe :
 
 
 
  1. [b][color=BLACK]([/color][/b]setq lst '[b][color=FUCHSIA]([/color][/b]51 41 83 10 60 32 46 84[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b][b][color=BLACK]([/color][/b]defun lby2 [b][color=FUCHSIA]([/color][/b]l / tmp[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while l   [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]cons [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]nth 0 l[b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]nth 1 l[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] tmp[b][color=MAROON])[/color][/b]           l [b][color=MAROON]([/color][/b]cdr l[b][color=MAROON])[/color][/b]           l [b][color=MAROON]([/color][/b]cdr l[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=FUCHSIA]([/color][/b]reverse tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 
 
I would recommend doing some basic error checking for these types of routine
 
ie is the length of the list an even number.
 
-David
回复

使用道具 举报

39

主题

1451

帖子

19

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1598
发表于 2022-7-6 07:40:04 | 显示全部楼层
  1. (defun every2 (l) (cond ((and (car l) (cadr l)) (cons (list (car l) (cadr l)) (every2 (cddr l))))((car l) (list l)) ))
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 07:43:08 | 显示全部楼层
  1. (defun g2 ( l ) (if l (cons (list (car l) (cadr l)) (g2 (cddr l)))))
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 07:47:56 | 显示全部楼层
 
Be careful with duplicates...
  1. _$ (every2nd '(1 2 3 1 2 3))(1 3 1 3)
For your method, consider perhaps:
  1. (defun f ( l / i )   (setq i -1)   (vl-remove-if '(lambda ( x ) (= 1 (rem (setq i (1+ i)) 2))) l))(defun g2 ( l ) (mapcar 'list (f l) (f (cdr l))))
  1. _$ (g2 '(1 2 3 1 2 3))((1 2) (3 1) (2 3))
回复

使用道具 举报

0

主题

375

帖子

385

银币

限制会员

铜币
-7
发表于 2022-7-6 07:49:54 | 显示全部楼层
Three alternatives
1 - recursive - similar to Lee (see l2p function)
 
2  - using foreach
  1. (defun ph1:l2p (l / s r) (foreach x l   (if s      (setq r (cons (list s x) r)            s nil      )      (setq s x)   ) ) (reverse r))
3 - using repeat
  1. (defun ph2:l2p (l / r) (repeat (/ (length l) 2)   (setq r (append r (list (list (car l) (cadr l))))         l (cddr l)   ) ) r)
回复

使用道具 举报

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-6 07:52:17 | 显示全部楼层
Uffff. I will explore all options, to see which one i choose.
I thought it would be easier with mapcar and lambda.
Thank you all.
Regards.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 07:57:27 | 显示全部楼层
 
Note that not all tasks are best solved using mapcar.
For this particular example, the items in the list need to be processed in pairs, hence mapcar is not best suited for the task since it will process every item in a list consecutively, meaning that you must first manipulate the list before passing it to mapcar. This extra work to manipulate the list is very inefficient where other methods could process the list directly.
 
The mapcar function is just one tool in your AutoLISP toolbox, from which you should choose the best tool for the job. Restricting yourself to solely using mapcar for every task would be like building a house with just a hammer...
回复

使用道具 举报

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-6 08:00:01 | 显示全部楼层
O.K. making way....Thanks
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-10 04:25 , Processed in 0.450984 second(s), 83 queries .

© 2020-2025 乐筑天下

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