乐筑天下

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

[编程交流] 列表的部分

[复制链接]

14

主题

71

帖子

16

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
119
发表于 2022-7-6 15:18:44 | 显示全部楼层
另一个:
  1. (defun remove-lst-items (lst itemlst / i)
  2. (setq i -1)
  3. (vl-remove-if '(lambda (x)
  4.                   (member (setq i (1+ i))
  5.                           itemlst
  6.                   )
  7.                 ) lst
  8. )
  9. )
  10. (defun hold-lst-items (lst itemlst / i)
  11. (setq i -1)
  12. (vl-remove-if-not '(lambda (x)
  13.                   (member (setq i (1+ i))
  14.                           itemlst
  15.                   )
  16.                 ) lst
  17. )
  18. )
  1. _$ (remove-lst-items '(a b c d e f) '(0 4 5))
  2. (B C D)
  3. _$ (hold-lst-items '(a b c d e f) '(0 4 5))
  4. (A E F)
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:21:45 | 显示全部楼层
很好的一个Taner,总是很高兴看到一个替代方法-从来没有想过会有这么多的解决方案来解决同一个问题。。。
回复

使用道具 举报

14

主题

71

帖子

16

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
119
发表于 2022-7-6 15:26:01 | 显示全部楼层
sublst有另一个代码。
  1. (defun remove-lstmn (m n lst / j)      ; remove the items  m~n
  2. (setq j -1)
  3. (vl-remove-if '(lambda (x)
  4.                   (and
  5.                     (setq j (1+ j))
  6.                     (<= m j)
  7.                     (>= n j)
  8.                   )
  9.                 ) lst
  10. )
  11. )
  12. (defun hold-lstmn (m n lst / j)        ; hold the items  m~n
  13. (setq j -1)
  14. (vl-remove-if-not '(lambda (x)
  15.                       (and
  16.                         (setq j (1+ j))
  17.                         (<= m j)
  18.                         (>= n j)
  19.                       )
  20.                     ) lst
  21. )
  22. )
  1. _$ lst
  2. (1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1)
  3. _$ (remove-lstmn 3 6 lst)
  4. (1 2 3 8 9 8 7 6 5 4 3 2 1)
  5. _$ (hold-lstmn 3 6 lst)
  6. (4 5 6 7)
  7. _$
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 15:28:42 | 显示全部楼层
另一个变体:
  1. ;;  CAB  10/15/2005
  2. ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
  3. ;;  Return a sub list starting at the nth position and
  4. ;;  with the number of items specified by num  0= all remaining items
  5. ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
  6. (defun nth+ (lst idx num / newlst)
  7. (and (or (numberp num) (setq num 0)) ; force all
  8.       (zerop num) ; if zero
  9.       (setq num (length lst)) ; all
  10. )
  11. (repeat num
  12.    (setq newlst (cons (nth idx lst) newlst)
  13.          idx (1+ idx))
  14. )
  15. (reverse (vl-remove nil newlst))
  16. )

 
  1. (defun c:test()
  2. (print (nth+ '(1 2 3 4 5) 0 2)) ; |-> (1 2)  
  3. (print (nth+ '(1 2 3 4 5) 3 2)) ; |-> (4 5)  
  4. (print (nth+ '(1 2 3 4 5) 4 5)) ; |-> (5)   
  5. (print (nth+ '(1 2 3 4 5) 6 2)) ; |-> nil   
  6. (print (nth+ '(1 2 3 4 5) 2 0)) ; |-> (3 4 5)
  7. (princ)
  8. )
回复

使用道具 举报

0

主题

119

帖子

119

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 15:31:58 | 显示全部楼层
假设我有一个列表:
 
  1. (setq alist '(a b b c))

 
如何检索“b”的值?
 
我是否必须在每个列表元素上使用“if”函数来与其余元素进行比较,还是有一种更短的方法?
回复

使用道具 举报

0

主题

119

帖子

119

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 15:34:35 | 显示全部楼层
 
好的,我正在学习mapcar函数,因为它似乎适用于此。
 
我在这里做什么?
 
  1. (setq alist '(a b b c))
  2. (setq list-index 0
  3.      listlength (length alist))
  4. ;;; determine if two elements of the list are equal
  5. (repeat listlength
  6. (setq element (nth list-index alist))
  7. (if (mapcar 'equal element alist) ; [b]<[color="DarkRed"] What's wrong with this[/color]?[/b]
  8.    (setq repeated_item element)
  9. )
  10. (setq list-index (1+ list-index))
  11. )
回复

使用道具 举报

2

主题

182

帖子

180

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:37:34 | 显示全部楼层
哦我懂了。mapcar创建迭代,这意味着我不需要重复函数。我真是个蠢货,我知道这比我想象的要容易得多。我还需要了解lambda。。。非常感谢你,Se7en。
回复

使用道具 举报

0

主题

119

帖子

119

银币

初来乍到

Rank: 1

铜币
0
发表于 2022-7-6 15:42:13 | 显示全部楼层
*lmao*
 
没问题。
 
Lambda只是一个匿名函数。其中“(defun name(/)”是一个命名函数,将其全部替换为“(lambda(/)”,您得到了一个没有名称的函数。
 
如果你在一个程序中多次需要一个函数,你可以给它起个名字,对吗?(定义名称(/)。。。
 
如果你只需要一次函数,你可以在这里使用(lambda(/)。了解了?
回复

使用道具 举报

2

主题

182

帖子

180

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:45:07 | 显示全部楼层
另一种解决方案:
  1. 18
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 15:48:14 | 显示全部楼层
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 21:59 , Processed in 0.419275 second(s), 70 queries .

© 2020-2025 乐筑天下

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