表中元素的替换
;;表中元素的替换---(支持内嵌5层表)的成员,但不支持点对表;;(replst "a" "b" '(("1a" 5 ((("c" "a"))) "wrf" "c")("a" ("a" "c"))));返回(("1a" 5 ((("c" "a"))) "wrf" "c") ("b" ("b" "c")))
;;在论坛中没有找到关于表中元素的替换,查找成员也只有member函数,但不支持表中表的元素。所以自己写了一个,但内嵌表层数方法不好,请大家帮忙修改一下。
(defun replst ( old new l / )
(cond ((equal l nil)
nil)
((equal old (car l))
(cons new (replst old new (cdr l))))
((and (listp (car l))
(member old (car l)))
(cons (replst old new (car l)) (replst old new (cdr l))))
((and (listp (car l))
(listp (car (car l)))
(member old (car (car l))))
(cons (cons (replst old new (car (car l)))
(replst old new (cdr (car l)))
)
(replst old new (cdr l))
))
((and (listp (car l))
(listp (car (car l)))
(listp (car (car (car l))))
(member old (car (car (car l)))))
(cons (cons (list (replst old new (car (car (car l)))))
(replst old new (cdr (car l)))
)
(replst old new (cdr l))
))
((and (listp (car l))
(listp (car (car l)))
(listp (car (car (car l))))
(listp (car (car (car (car l)))))
(member old (car (car (car (car l))))))
(cons (cons (list (list (replst old new (car (car (car (car l)))))))
(replst old new (cdr (car l)))
)
(replst old new (cdr l))
))
((and (listp (car l))
(listp (car (car l)))
(listp (car (car (car l))))
(listp (car (car (car (car l)))))
(listp (car (car (car (car l)))))
(member old (car (car (car (car (car l)))))))
(cons (cons (list (list (list (replst old new (car (car (car (car (car l)))))))))
(replst old new (cdr (car l)))
)
(replst old new (cdr l))
))
(T
(cons (car l) (replst old new (cdr l))))
)
)
;函数:SubstItem
;功能:替换表中所有旧项(支持任意嵌套表)
;函数代码:
;;;By xq4u 2011-7-24
(defun SubstItem (newitem olditem lst / x tmplst)
(foreach x (Subst newitem olditem lst)
(if (listp x)
(setq tmplst (append tmplst (list (SubstItem newitem olditem x))))
(setq tmplst (append tmplst (list x)))
)
)
tmplst
)
;语法:
;(SubstItem newitem olditem lst)
;参数:
;newitem:要替换到的内容
;olditem:被查找的内容
;lst:要替换的表
;返回值:
;返回修改后所得的表
;说明:在表中搜索某旧项,并将表中出现的每一个旧项用新项代替,然后返回修改后所得的表(支持任意嵌套表) wzg356 发表于 2021-9-26 09:09
;函数:SubstItem
;功能:替换表中所有旧项(支持任意嵌套表)
;函数代码:
非常感谢,原来是我没有搜到。
页:
[1]