Page 1 of 1

### is there a way to replace the elements in a list?

Posted: Tue Jul 12, 2011 10:55 pm
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?

Posted: Tue Jul 12, 2011 11:10 pm
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.

Are you doing the L-99? (You probably should; you'd be able to answer this question yourself by #15 or so.)

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-REPLACECL-PIXIE> (dumb-replace (list 'a 'b 'c 'd) 'bork)(BORK BORK BORK BORK)`

...But it can change to suit a variety of problems, as you'll see if you're doing the L-99.

### Re: is there a way to replace the elements in a list?

Posted: Wed Jul 13, 2011 12:25 am
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))`

Cheers

### Re: is there a way to replace the elements in a list?

Posted: Wed Jul 13, 2011 10:34 am
@marcoxa and Duke: I think you guys misunderstood him, he wants to destructively replace the elements.

@yesimthetaxman: What you want is the function mapl:

Code: Select all
`(defun my-replace (elt list)  (mapl #'(lambda (sublist)            (setf (car sublist) elt))    list))`

Note: I didn't test the code.

### Re: is there a way to replace the elements in a list?

Posted: Wed Jul 13, 2011 10:55 am
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.

### Re: is there a way to replace the elements in a list?

Posted: Wed Jul 13, 2011 11:59 am

### Re: is there a way to replace the elements in a list?

Posted: Thu Jul 14, 2011 5:17 am
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.

I gave you the answer using MAP. The version with MAPCAR is
Code: Select all
`(defun transmogrify (l &optional (x 42))    (mapcar (constantly x) l))`

A destructive version is

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))LLCL-USER 3 > (transmogrify/d ll)(42 42 42 42)CL-USER 4 > ll(42 42 42 42)`

Using the MAP version will hint you at generalizing the function so that it works with strings and vectors.

Cheers

### Re: is there a way to replace the elements in a list?

Posted: Thu Jul 14, 2011 10:53 pm
thank youuuuuuuuu