Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Q: what idiom for inserting an element in a list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robert Strandh  
View profile  
 More options Aug 10 2002, 11:30 am
Newsgroups: comp.lang.lisp
From: Robert Strandh <stra...@labri.u-bordeaux.fr>
Date: 10 Aug 2002 17:32:42 +0200
Local: Sat, Aug 10 2002 11:32 am
Subject: Q: what idiom for inserting an element in a list
Hello,

I am looking for an idiom for inserting an element (let's say
destructively) at a certain position in a list.  Even better, to
splice in a list at a certain position in an other list.  

For instance:

* (setf *l* (list 1 2 3 4 5 6 7))

(1 2 3 4 5 6 7)
* (insert-by-position 'a *l* 3)

(1 2 3 A 4 5 6 7)
* *l*

(1 2 3 A 4 5 6 7)
*

Is there an existing idiom that I should use?

--
Robert Strandh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kent M Pitman  
View profile  
 More options Aug 10 2002, 1:55 pm
Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: Sat, 10 Aug 2002 17:54:55 GMT
Local: Sat, Aug 10 2002 1:54 pm
Subject: Re: Q: what idiom for inserting an element in a list

Don't look for idioms.

Make functions.

If you later find your function can be implemented more efficiently, do so
centrally.  But don't make clever use of some idiomatic thing to avoid writing
a function with a useful name.

[Incidentally, don't expect that insertion can work by side-effect.  
 Insertion at position 0 will not be able to.  Always do
   (setq *l* (insert-by-position 'a *l* 3))
 and make sure INSERT-BY-POSITION returns the right value to make this work.
 (Although you might want to check out DEFINE-MODIFY-MACRO if you want a
 macro interface.)]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Moore  
View profile  
 More options Aug 11 2002, 1:45 am
Newsgroups: comp.lang.lisp
From: tmo...@sea-tmoore-l.dotcast.com (Tim Moore)
Date: 11 Aug 2002 05:45:42 GMT
Local: Sun, Aug 11 2002 1:45 am
Subject: Re: Q: what idiom for inserting an element in a list
On 10 Aug 2002 17:32:42 +0200, Robert Strandh <stra...@labri.u-bordeaux.fr>
wrote:

Idiomatic or not, here's what I'd do:

(defun insert-by-position (val list pos)
  (if (zerop pos)
      (cons val list)
    (let ((old-tail (nthcdr (1- pos) list)))
      (setf (cdr old-tail) (cons val (cdr old-tail)))
      list)))

Tim


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Bourguignon  
View profile  
 More options Aug 11 2002, 4:43 am
Newsgroups: comp.lang.lisp
From: Pascal Bourguignon <too.much.s...@example.com>
Date: 11 Aug 2002 10:42:58 +0200
Local: Sun, Aug 11 2002 4:42 am
Subject: Re: Q: what idiom for inserting an element in a list

Which is  not very regular:

    - (insert-by-position 'A '(1 2 3) 2) won't work generaly (you
      can't change the cdr of a literal list such as '(1 2 3)).

    - (insert-by-position 'A *l* 0) does not change *l* while
      (insert-by-position 'A *l* 1) does. This is quite gratuitous.

Personnaly, I prefer:

(defun insert-by-position-r (val list pos)
  (if (= pos 0)
      (cons val list)
    (cons (car list) (insert-by-position val (cdr list) (1- pos))))
  );;insert-by-position

To be used as:  (setq new-list (insert-by-position-r (val old-list pos)))
Otherwise, a macro is in order, to handle the case val=0.

--
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Strandh  
View profile  
 More options Aug 11 2002, 5:30 am
Newsgroups: comp.lang.lisp
From: Robert Strandh <stra...@labri.u-bordeaux.fr>
Date: 11 Aug 2002 11:30:08 +0200
Local: Sun, Aug 11 2002 5:30 am
Subject: Re: Q: what idiom for inserting an element in a list

tmo...@sea-tmoore-l.dotcast.com (Tim Moore) writes:
> Idiomatic or not, here's what I'd do:

> (defun insert-by-position (val list pos)
>   (if (zerop pos)
>       (cons val list)
>     (let ((old-tail (nthcdr (1- pos) list)))
>       (setf (cdr old-tail) (cons val (cdr old-tail)))
>       list)))

OK, that's very close to the one I finally settled on.  Though, I used
`push' instead of `setf'.

Thanks,
--
Robert Strandh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Strandh  
View profile  
 More options Aug 11 2002, 5:30 am
Newsgroups: comp.lang.lisp
From: Robert Strandh <stra...@labri.u-bordeaux.fr>
Date: 11 Aug 2002 11:38:00 +0200
Subject: Re: Q: what idiom for inserting an element in a list
Kent M Pitman <pit...@world.std.com> writes:

> Don't look for idioms.

> Make functions.

I was thinking of doing both.  Writing a function that uses the
idiom.

> If you later find your function can be implemented more efficiently, do so
> centrally.  But don't make clever use of some idiomatic thing to avoid writing
> a function with a useful name.

Sure.

> [Incidentally, don't expect that insertion can work by side-effect.  
>  Insertion at position 0 will not be able to.  Always do
>    (setq *l* (insert-by-position 'a *l* 3))
>  and make sure INSERT-BY-POSITION returns the right value to make this work.

Yes, I actually knew that.  Thanks.
--
Robert Strandh

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Strandh  
View profile  
 More options Aug 11 2002, 5:30 am
Newsgroups: comp.lang.lisp
From: Robert Strandh <stra...@labri.u-bordeaux.fr>
Date: 11 Aug 2002 11:35:12 +0200
Local: Sun, Aug 11 2002 5:35 am
Subject: Re: Q: what idiom for inserting an element in a list

Tim's solution is `destructive' (which means exactly that you can't
change literals and that it has this `gratuitous' behavior), which is
what I asked for.  

--
Robert Strandh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel Ray Holveck  
View profile  
 More options Aug 11 2002, 6:30 am
Newsgroups: comp.lang.lisp
From: Joel Ray Holveck <jo...@juniper.net>
Date: 11 Aug 2002 03:18:49 -0700
Local: Sun, Aug 11 2002 6:18 am
Subject: Re: Q: what idiom for inserting an element in a list
>> (defun insert-by-position (val list pos)
[snip]
> Which is  not very regular:
>     - (insert-by-position 'A '(1 2 3) 2) won't work generaly (you
>       can't change the cdr of a literal list such as '(1 2 3)).
>     - (insert-by-position 'A *l* 0) does not change *l* while
>       (insert-by-position 'A *l* 1) does. This is quite gratuitous.
[snip]
> Personnaly, I prefer:
> (defun insert-by-position-r (val list pos)

[snip]

The situation is no different with SORT, or any other destructive
function.

However, we now have an implementation of both INSERT-BY-POSITION and
NINSERT-BY-POSITION (renamed for regularity with most other CL
destructive functions), and the user can use whichever he finds most
appropriate-- remembering all the normal caveats associated with
destructive functions.

joelh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hrvoje Niksic  
View profile  
 More options Aug 11 2002, 10:17 pm
Newsgroups: comp.lang.lisp
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Mon, 12 Aug 2002 04:17:53 +0200
Local: Sun, Aug 11 2002 10:17 pm
Subject: Re: Q: what idiom for inserting an element in a list

Hardly idiomatic, but my first impulse was to try something like this:

    (push 'a (nthcdr 3 *l*))

But Common Lisp doesn't seem to define a SETF method for NTHCDR.  I
wonder why?  Emacs's `cl.el' defines it, and this example works there.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Aug 12 2002, 2:45 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 12 Aug 2002 06:45:57 +0000
Local: Mon, Aug 12 2002 2:45 am
Subject: Re: Q: what idiom for inserting an element in a list
* Hrvoje Niksic <hnik...@xemacs.org>
| Hardly idiomatic, but my first impulse was to try something like this:
|
|     (push 'a (nthcdr 3 *l*))
|
| But Common Lisp doesn't seem to define a SETF method for NTHCDR.  I
| wonder why?  Emacs's `cl.el' defines it, and this example works there.

  You would of course use (setf (cdr (nthcdr ...)) ...).

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Aug 12 2002, 10:52 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Mon, 12 Aug 2002 14:52:51 GMT
Local: Mon, Aug 12 2002 10:52 am
Subject: Re: Q: what idiom for inserting an element in a list
In article <3238123557311...@naggum.no>, Erik Naggum  <e...@naggum.no> wrote:

>* Hrvoje Niksic <hnik...@xemacs.org>
>| Hardly idiomatic, but my first impulse was to try something like this:
>|
>|     (push 'a (nthcdr 3 *l*))
>|
>| But Common Lisp doesn't seem to define a SETF method for NTHCDR.  I
>| wonder why?  Emacs's `cl.el' defines it, and this example works there.

>  You would of course use (setf (cdr (nthcdr ...)) ...).

Except that doesn't work for the 0 case.  If SETF of NTHCDR were defined,
it could expand (setf (nthcdr n l) x) into:

(if (zerop n)
    (setf l x)
    (setf (cdr (nthcdr (1- n))) x))

(with appropriate stuff to fix order of evaluation and prevent multiple
evaluation, of course), rather than requiring the user to special case 0.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »