yesimthetaxman wrote:I don't actually mean destructively replace, I mean lets say you pass the list (a b c d e), I want to return the list (8 8 8 8 8)... so basically I want a list returned with the same number of elements but instead of the original items, it'd be an item of my choosing. I am able to do this using the replace function, but I am wondering if there is a way to implement this using something like mapcar or reduce.
CL-PIXIE> (defun dumb-replace (input-list substitution)
(labels ((rec (lst acc)
(if (null lst)
acc
(rec (cdr lst) (cons substitution acc)))))
(rec input-list nil)))
DUMB-REPLACE
CL-PIXIE> (dumb-replace (list 'a 'b 'c 'd) 'bork)
(BORK BORK BORK BORK)
yesimthetaxman wrote:I don't actually mean destructively replace, I mean lets say you pass the list (a b c d e), I want to return the list (8 8 8 8 8)... so basically I want a list returned with the same number of elements but instead of the original items, it'd be an item of my choosing. I am able to do this using the replace function, but I am wondering if there is a way to implement this using something like mapcar or reduce.
(defun transfomgrify (list &optional (e 42))
(map 'list (constantly e) list))(defun my-replace (elt list)
(mapl #'(lambda (sublist)
(setf (car sublist) elt))
list))gugamilare wrote:@marcoxa and Duke: I think you guys misunderstood him, he wants to destructively replace the elements.
yesimthetaxman wrote:I don't actually mean destructively replace.

yesimthetaxman wrote:I don't actually mean destructively replace, I mean lets say you pass the list (a b c d e), I want to return the list (8 8 8 8 8)... so basically I want a list returned with the same number of elements but instead of the original items, it'd be an item of my choosing. I am able to do this using the replace function, but I am wondering if there is a way to implement this using something like mapcar or reduce.
(defun transmogrify (l &optional (x 42))
(mapcar (constantly x) l))
(defun transmogrify/d (l &optional (x 42))
(map-into l (constantly x) l))
CL-USER 2 > (defvar ll (list 1 2 3 4))
LL
CL-USER 3 > (transmogrify/d ll)
(42 42 42 42)
CL-USER 4 > ll
(42 42 42 42)
Users browsing this forum: Google [Bot] and 3 guests