replace slice of list to a element

For the Compleat Fan
Locked
ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

replace slice of list to a element

Post 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?

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: replace slice of list to a element

Post 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)
(λx. x x) (λx. x x)

ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: replace slice of list to a element

Post by ssqq »

Sorry, I have test my code.

Thanks a lots.

Locked