乐筑天下

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

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

[复制链接]

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-6 08:02:52 | 显示全部楼层
 
You're right as always Lee... So my first code :
  1. (defun every2nd ( lst / x ) (setq x -1) (vl-remove nil (mapcar '(lambda ( a ) (if (eq (rem (setq x (1+ x)) 2) 0) a)) lst)))(defun groupby2 ( lst ) (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst))))(groupby2 '(1 2 3 1 2 3))
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 08:05:25 | 显示全部楼层
No need for the lambda here :
 
  1. (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst)))
 
  1. (mapcar 'list (every2nd lst) (every2nd (cdr lst)))
回复

使用道具 举报

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-6 08:10:05 | 显示全部楼层
I can not see post # 11 and following.
  I get this message:
 
  1. [b]Forbidden[/b]You don't have permission to access /forum/showthread.php on this server.Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
回复

使用道具 举报

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-6 08:12:51 | 显示全部楼层
Now I can see them.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 08:14:40 | 显示全部楼层
 
This is because you have used "...." in your thread title.
回复

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-6 08:18:20 | 显示全部楼层
So, is this the shortest ?
 
  1. (defun every2nd ( lst / x ) (setq x -1) (vl-remove-if-not '(lambda ( a ) (eq (rem (setq x (1+ x)) 2) 0)) lst))(defun groupby2 ( lst ) (mapcar 'list (every2nd lst) (every2nd (cdr lst))))(groupby2 '(1 2 3 1 2 3))
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 08:22:22 | 显示全部楼层
 
If you were to remain using the vl-remove-if-not function (or vl-remove-if function, as per my earlier post), then those arrangements would most likely be the shortest.
 
Shorter code could obviously be obtained by using alternative methods to generate each list supplied to mapcar:
  1. (defun f ( l ) (if l (cons (car l) (f (cddr l)))))(defun g ( l ) (mapcar 'list (f l) (f (cdr l))))
...Or of course by abandoning mapcar as stated and demonstrated in my earlier posts.
回复

使用道具 举报

31

主题

95

帖子

65

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

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

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-6 08:28:55 | 显示全部楼层
  1. ;; Group by number by M.R. ;;(defun gn ( l n / f g k q )  (setq k n q l) (defun f ( l n ) (if (and l (> n 0)) (cons (car l) (f (setq q (setq l (cdr l))) (setq n (1- n)))))) (defun g ( l n ) (if q (cons (f q k) (g q k)))) (g q k)  )(gn '(11 21 31 12 22 32) 2)
 
M.R.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 08:30:36 | 显示全部楼层
 
Since you are calling the function f recursively, the use of setq (as noted the following expression) is unnecessary, as the values of the symbols l and n are passed as arguments to the f function.
 
  1. (f (setq q [highlight](setq[/highlight] l (cdr l))) [highlight](setq[/highlight] n (1- n)))
Also, by renaming the parameters of the f function, you could remove the need for the extra 'state' variables k & q, e.g.:
 
  1. (defun gn ( l n / f g )   (defun f ( a b )       (if (and a (< 0 b))           (cons (car a) (f (setq l (cdr a)) (1- b)))       )   )   (defun g ( l n ) (if l (cons (f l n) (g l n))))   (g l n))
However, note further that the function g is also superfluous since it is identical to the function in which it is defined:
 
  1. (defun gn ( l n / f )   (defun f ( a b )       (if (and a (< 0 b))           (cons (car a) (f (setq l (cdr a)) (1- b)))       )   )   (if l (cons (f l n) (gn l n))))
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-10 04:38 , Processed in 0.412121 second(s), 70 queries .

© 2020-2025 乐筑天下

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