(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)
很好的一个Taner,总是很高兴看到一个替代方法-从来没有想过会有这么多的解决方案来解决同一个问题。。。 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)
_$ 另一个变体:
;;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)
) 假设我有一个列表:
(setq alist '(a b b c))
如何检索“b”的值?
我是否必须在每个列表元素上使用“if”函数来与其余元素进行比较,还是有一种更短的方法?
好的,我正在学习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))
) 哦我懂了。mapcar创建迭代,这意味着我不需要重复函数。我真是个蠢货,我知道这比我想象的要容易得多。我还需要了解lambda。。。非常感谢你,Se7en。 *lmao*
没问题。
Lambda只是一个匿名函数。其中“(defun name(/)”是一个命名函数,将其全部替换为“(lambda(/)”,您得到了一个没有名称的函数。
如果你在一个程序中多次需要一个函数,你可以给它起个名字,对吗?(定义名称(/)。。。
如果你只需要一次函数,你可以在这里使用(lambda(/)。了解了? 另一种解决方案:
18
页:
1
[2]