Page 1 of 1

replace slice of list to a element

Posted: Wed Jun 11, 2014 2:33 pm
by ssqq
As manual said:
Slices or rest parts of lists or arrays as used in implicit resting or slicing cannot be substituted at once using setf, but would have to be substituted element by element.
.

If I want replace a piece of list to a element:

Code: Select all

(set 'lst '(a b c d e f)
(replace-slice (slice lst 2 3) 'g)
lst --> '(a b g f)
I use:

Code: Select all

(append (nth '(0 1) lst) 'g (nth 5 lst))
If have any other simple way to get result?

Re: replace slice of list to a element

Posted: Wed Jun 11, 2014 5:07 pm
by rickyboy
ssqq wrote:I use:

Code: Select all

(append (nth '(0 1) lst) 'g (nth 5 lst))
If have any other simple way to get result?
That append expression won't even work.

Code: Select all

> (append (nth '(0 1) lst) 'g (nth 5 lst))

ERR: array, list or string expected : a
However, the following will work.

Code: Select all

> (append (slice lst 0 2) (list 'g) (slice lst 5))
(a b g f)
> ;; or, with implicits:
> (append (0 2 lst) (list 'g) (5 lst))
(a b g f)

Re: replace slice of list to a element

Posted: Thu Jun 12, 2014 3:11 am
by ssqq
Sorry, I have test my code.

Thanks a lots.