is there a way to replace the elements in a list?
-
- Posts: 10
- Joined: Thu Jul 07, 2011 7:02 pm
is there a way to replace the elements in a list?
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.
Re: is there a way to replace the elements in a list?
Are you doing the L-99? (You probably should; you'd be able to answer this question yourself by #15 or so.)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.
One way to do it is with a recursive function. The form to do the rather silly thing you ask looks like this:
Code: Select all
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)
"If you want to improve, be content to be thought foolish and stupid." -Epictetus
Re: is there a way to replace the elements in a list?
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.
Code: Select all
(defun transfomgrify (list &optional (e 42))
(map 'list (constantly e) list))
Marco Antoniotti
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: is there a way to replace the elements in a list?
@marcoxa and Duke: I think you guys misunderstood him, he wants to destructively replace the elements.
@yesimthetaxman: What you want is the function mapl:
Note: I didn't test the code.
@yesimthetaxman: What you want is the function mapl:
Code: Select all
(defun my-replace (elt list)
(mapl #'(lambda (sublist)
(setf (car sublist) elt))
list))
-
- Posts: 59
- Joined: Sun May 29, 2011 8:52 pm
- Location: NY
- Contact:
Re: is there a way to replace the elements in a 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.
I don't think he wants it to be destructively replaced.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: is there a way to replace the elements in a list?
I gave you the answer using MAP. The version with MAPCAR isyesimthetaxman 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.
Code: Select all
(defun transmogrify (l &optional (x 42))
(mapcar (constantly x) l))
Code: Select all
(defun transmogrify/d (l &optional (x 42))
(map-into l (constantly x) l))
Code: Select all
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)

Cheers
Marco Antoniotti
-
- Posts: 10
- Joined: Thu Jul 07, 2011 7:02 pm
Re: is there a way to replace the elements in a list?
thank youuuuuuuuu