Here is another which I think is cool.'(t a) is in the wrong place
http://www.lee-mac.com/substn.htmlthank you.
the code is still here at theswamp, one of the first Evgeniy's posts :)
it also has a little drawback: (multi-subst (list 'a 'b 'c) (list (list 'a 1)))
on nested tree structuresmy remark has nothing to do with "nested tree structures"
_$ (multi-subst (list 'a 'b 'c) (list (list 'a 1)))
(1 1 1) - bad
_$ (multi-subst (list "a" "b" "c") (list (list "a" 1)))
(1 "b" "c") - good
(list 'quote (car a))
will fix that_$ (multi-subst '(a ("b") "c") '((a 1) (("b") 2) ("c" 3)))
(1 2 3) - good
Ah! ...well, I went down the wrong path didn't I?not wrong but different :)
(replace--8 nil 2 '(1 nil (3 4) (5 6 ((3))) (3)))
almost forgot ;)Oh well yeah of course that will give some really fun results. ...that’s a feature *wink*.Code: [Select](replace--8 nil 2 '(1 nil (3 4) (5 6 ((3))) (3)))
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
; Marc'Antonio Alessi - ; Version 2.02 - 15/02/2008 > old name: ALE_SubstNth
(cond
( (null In_Lst) nil )
( (zerop NthPos) (cons NewItm (cdr In_Lst)) )
( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
( (zerop (setq LstLng (- LstLng (1+ NthPos))))
(append (reverse (cdr InRLst)) (list NewItm))
)
( T
(setq OldItm (nth NthPos In_Lst))
(while (/= NthPos (length (setq InRLst (cdr (member OldItm InRLst))))))
(while (/= LstLng (length (setq In_Lst (cdr (member OldItm In_Lst))))))
(append (reverse InRLst) (cons NewItm In_Lst))
)
)
)
(defun ALE_List_SubstNth_NoRmv (NthPos NewItm In_Lst / LstLng SttLst)
; Marc'Antonio Alessi - 2005
(cond
( (> (/ (setq LstLng (length In_Lst)) 2) NthPos)
(repeat NthPos
(setq
SttLst (cons (car In_Lst) SttLst)
In_Lst (cdr In_Lst)
)
)
(append (reverse SttLst) (cons NewItm (cdr In_Lst)))
)
( (> (1+ NthPos) LstLng) In_Lst )
( T
(setq In_Lst (reverse In_Lst))
(repeat (1- (- LstLng NthPos))
(setq
SttLst (cons (car In_Lst) SttLst)
In_Lst (cdr In_Lst)
)
)
(append (reverse (cdr In_Lst)) (cons NewItm SttLst))
)
)
)
(benchmark
'(
(replace--1 lst 3 "A")
(replace--2 lst 3 "A")
(ALE_List_SubstNth 3 "A" lst (reverse Lst))
(ALE_List_SubstNth_NoRmv 3 "A" lst )
(LM:SubstNth "A" 3 lst)
(LM:SubstNth "A" 3 lst)
(ALE_List_SubstNth_NoRmv 3 "A" lst )
(ALE_List_SubstNth 3 "A" lst (reverse Lst))
(replace--2 lst 3 "A")
(replace--1 lst 3 "A")
)
)
(REPLACE--2 LST 3 "A").......................1734 / 1.45 <fastest>
(LM:SUBSTNTH "A" 3 LST)......................1735 / 1.45
(LM:SUBSTNTH "A" 3 LST)......................1765 / 1.42
(REPLACE--2 LST 3 "A").......................1766 / 1.42
(ALE_LIST_SUBSTNTH_NORMV 3 "A" LST)..........1844 / 1.36
(ALE_LIST_SUBSTNTH_NORMV 3 "A" LST)..........1985 / 1.27
(ALE_LIST_SUBSTNTH 3 "A" LST (REVERS...).....2187 / 1.15
(ALE_LIST_SUBSTNTH 3 "A" LST (REVERS...).....2219 / 1.13
(REPLACE--1 LST 3 "A").......................2500 / 1.01
(REPLACE--1 LST 3 "A").......................2515 / 1 <slowest>
--- Benchmark utility: In memory of Michael Puckett ---