taner 发表于 2022-7-6 15:18:44

另一个:
(defun remove-lst-items (lst itemlst / i)
(setq i -1)
(vl-remove-if '(lambda (x)
                  (member (setq i (1+ i))
                        itemlst
                  )
                ) lst
)
)
(defun hold-lst-items (lst itemlst / i)
(setq i -1)
(vl-remove-if-not '(lambda (x)
                  (member (setq i (1+ i))
                        itemlst
                  )
                ) lst
)
)

_$ (remove-lst-items '(a b c d e f) '(0 4 5))
(B C D)
_$ (hold-lst-items '(a b c d e f) '(0 4 5))
(A E F)

Lee Mac 发表于 2022-7-6 15:21:45

很好的一个Taner,总是很高兴看到一个替代方法-从来没有想过会有这么多的解决方案来解决同一个问题。。。

taner 发表于 2022-7-6 15:26:01

sublst有另一个代码。
(defun remove-lstmn (m n lst / j)      ; remove the itemsm~n
(setq j -1)
(vl-remove-if '(lambda (x)
                  (and
                  (setq j (1+ j))
                  (<= m j)
                  (>= n j)
                  )
                ) lst
)
)
(defun hold-lstmn (m n lst / j)      ; hold the itemsm~n
(setq j -1)
(vl-remove-if-not '(lambda (x)
                      (and
                        (setq j (1+ j))
                        (<= m j)
                        (>= n j)
                      )
                  ) lst
)
)
_$ lst
(1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1)
_$ (remove-lstmn 3 6 lst)
(1 2 3 8 9 8 7 6 5 4 3 2 1)
_$ (hold-lstmn 3 6 lst)
(4 5 6 7)
_$

CAB 发表于 2022-7-6 15:28:42

另一个变体:
;;CAB10/15/2005
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
;;Return a sub list starting at the nth position and
;;with the number of items specified by num0= all remaining items
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++
(defun nth+ (lst idx num / newlst)
(and (or (numberp num) (setq num 0)) ; force all
      (zerop num) ; if zero
      (setq num (length lst)) ; all
)
(repeat num
   (setq newlst (cons (nth idx lst) newlst)
         idx (1+ idx))
)
(reverse (vl-remove nil newlst))
)
 
(defun c:test()
(print (nth+ '(1 2 3 4 5) 0 2)) ; |-> (1 2)
(print (nth+ '(1 2 3 4 5) 3 2)) ; |-> (4 5)
(print (nth+ '(1 2 3 4 5) 4 5)) ; |-> (5)   
(print (nth+ '(1 2 3 4 5) 6 2)) ; |-> nil   
(print (nth+ '(1 2 3 4 5) 2 0)) ; |-> (3 4 5)
(princ)
)

uddfl 发表于 2022-7-6 15:31:58

假设我有一个列表:
 
(setq alist '(a b b c))
 
如何检索“b”的值?
 
我是否必须在每个列表元素上使用“if”函数来与其余元素进行比较,还是有一种更短的方法?

uddfl 发表于 2022-7-6 15:34:35

 
好的,我正在学习mapcar函数,因为它似乎适用于此。
 
我在这里做什么?
 
(setq alist '(a b b c))
(setq list-index 0
   listlength (length alist))

;;; determine if two elements of the list are equal
(repeat listlength
(setq element (nth list-index alist))
(if (mapcar 'equal element alist) ; < What's wrong with this?
   (setq repeated_item element)
)
(setq list-index (1+ list-index))
)

Se7en 发表于 2022-7-6 15:37:34

哦我懂了。mapcar创建迭代,这意味着我不需要重复函数。我真是个蠢货,我知道这比我想象的要容易得多。我还需要了解lambda。。。非常感谢你,Se7en。

uddfl 发表于 2022-7-6 15:42:13

*lmao*
 
没问题。
 
Lambda只是一个匿名函数。其中“(defun name(/)”是一个命名函数,将其全部替换为“(lambda(/)”,您得到了一个没有名称的函数。
 
如果你在一个程序中多次需要一个函数,你可以给它起个名字,对吗?(定义名称(/)。。。
 
如果你只需要一次函数,你可以在这里使用(lambda(/)。了解了?

Se7en 发表于 2022-7-6 15:45:07

另一种解决方案:
18

CAB 发表于 2022-7-6 15:48:14

页: 1 [2]
查看完整版本: 列表的部分