From: nicolas.edel on
CL has find, position, remove, subsitute, and others. Why no insert
(at least for list) ?
From: Stanisław Halik on
thus spoke nicolas.edel(a)gmail.com:

> CL has find, position, remove, subsitute, and others. Why no insert
> (at least for list) ?

Sure it has. See PUSH and tail-consing. Note that PUSH doesn't modify
the original list, if it's passed as an argument, the change won't
propagate by itself into the calling function.

--
Nawet świnka wejdzie na drzewo kiedy ją chwalą.
From: Pascal J. Bourguignon on
nicolas.edel(a)gmail.com writes:

> CL has find, position, remove, subsitute, and others. Why no insert
> (at least for list) ?

Basically, because there is no list in lisp.

See the lisp paradoxes in: http://www.informatimago.com/usenet.html



Otherwise, insert could (very) naively be implemented as:

(defun insert (value index list)
(push value (nthcdr index list)))

so the reason why there is no INSERT is the same as why there is no
(SETF NTHCDR). See:
http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/e97408eed2a8d7d9/576860b23df03370?lnk=st&q=

Namely: (insert 'head 0 list) cannot be defined.

(let ((list (list 'neck 'body 'leg)))
(let ((a list)
(b list))
(insert 'head 0 list)
;; what would you get from:
(print a)
(print b)
(print list)
;; ?
(values)))



Ok, so you could define an insert similar to delete:

(defun insert (value index sequence)
(etypecase sequence
(null (list value))
(list (if (zerop index)
(cons value sequence)
(progn (push value (cdr (nthcdr (1- index) sequence)))
sequence)))
(vector '\...)))



C/USER[32]> (let ((list (list 1 2 3 4 5 6 7)))
(setf list (insert pi 3 list))
(print list)
(setf list (insert -1 0 list))
(print list)
(values))

(1 2 3 3.1415926535897932385L0 4 5 6 7)
(-1 1 2 3 3.1415926535897932385L0 4 5 6 7)


But this is about as far as you can go, without a true list ADT.

If you really need to insert elements inside a list, then you should
define a list data type to let you do so easily, efficiently, and
meaningfully. Definitely with identity, perhaps with double-links, etc.

--
__Pascal Bourguignon__
From: Pascal J. Bourguignon on
Stanisław Halik <sthalik+usenet(a)tehran.lain.pl> writes:

> thus spoke nicolas.edel(a)gmail.com:
>
>> CL has find, position, remove, subsitute, and others. Why no insert
>> (at least for list) ?
>
> Sure it has. See PUSH and tail-consing. Note that PUSH doesn't modify
> the original list,

Not always true. PUSH modifies the place you give it. If this places
is a cell in the list, PUSH will modify that list. See my other answer.

> if it's passed as an argument, the change won't
> propagate by itself into the calling function.

--
__Pascal Bourguignon__
From: nicolas.edel on
> Basically, because there is no list in lisp.
> See the lisp paradoxes in:http://www.informatimago.com/usenet.html

There is no list *but* you may use 'list with the typep function ?
Hum ... I'll have follow the white^^link and better understand your
meaning ;)

> so the reason why there is no INSERT is the same as why there is no
> (SETF NTHCDR). See:http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/e9...

Ok, I'll investigate on this to understand the issues.

> Ok, so you could define an insert similar to delete:
> [...]

Well, I know it's not that hard to define the functions I need by
myself, but I was wondering why the standard lacks them. I'll follow
the links and read them twice.

Thanks.

-Nicolas