unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* make an element last element of a list
@ 2003-10-21 15:08 Laurent Marzullo
  2003-10-21 15:42 ` Stephen Compall
  2003-10-21 15:50 ` rm
  0 siblings, 2 replies; 5+ messages in thread
From: Laurent Marzullo @ 2003-10-21 15:08 UTC (permalink / raw)


Hello all,

I've got a scheme list 

SCM	scm_game_list;

and I want to add, with C API, an elements at the end of
the list.

Is there any function to do this or must I go throw the entire
list and adding a new scm_cons( new_elem , SCM_EOL ) at the end ?

Thanks
-- 
Laurent Marzullo <marzullo@la-defense.oilfield.slb.com>



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: make an element last element of a list
  2003-10-21 15:08 make an element last element of a list Laurent Marzullo
@ 2003-10-21 15:42 ` Stephen Compall
  2003-10-21 15:50 ` rm
  1 sibling, 0 replies; 5+ messages in thread
From: Stephen Compall @ 2003-10-21 15:42 UTC (permalink / raw)
  Cc: Guile (Devel)

Laurent Marzullo <marzullo@la-defense.oilfield.slb.com> writes:

> I've got a scheme list 
> 
> SCM	scm_game_list;
> 
> and I want to add, with C API, an elements at the end of
> the list.
> 
> Is there any function to do this or must I go throw the entire
> list and adding a new scm_cons( new_elem , SCM_EOL ) at the end ?

#ifdef HUMBLE_OPINION
Maybe you don't want to hear this, but how are you building the list?
If you must have access to the list in the order in which you seem to
be working on it above *before* you add elements to the list, then you
ought to keep track of the last cell, as well as scm_game_list.

If you are building the list first, then using it, but still need the
order, then it would be best to cons new elements onto the front, then
use scm_reverse_x (saving the return value).

If you don't care about the order at all, just cons new items onto the
beginning.
#endif

--
Stephen Compall or s11 or sirian

I'm not proud.

asset Freeh TELINT ANC Baranyi MD4 Lon Horiuchi cryptanalysis PGP bomb
LLNL quiche halcon SRI colonel


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: make an element last element of a list
  2003-10-21 15:08 make an element last element of a list Laurent Marzullo
  2003-10-21 15:42 ` Stephen Compall
@ 2003-10-21 15:50 ` rm
  2003-10-22 10:22   ` tomas
  1 sibling, 1 reply; 5+ messages in thread
From: rm @ 2003-10-21 15:50 UTC (permalink / raw)
  Cc: Guile (Devel)

On Tue, Oct 21, 2003 at 05:08:42PM +0200, Laurent Marzullo wrote:
> Hello all,
> 
> I've got a scheme list 
> 
> SCM	scm_game_list;
> 
> and I want to add, with C API, an elements at the end of
> the list.

If you want to destrutively modify your list you can do the
following:
 
 (define my-lst (list 1 2 3 4))
 (define new-last (list 5))
 (set-cdr! (last my-lst) new-last)

using the c functions 
 

 SCM new_last =  scm_cons( new_elem , SCM_EOL );

 scm_set_cdr_x ( scm_last_pair (my_lst),  new_last );

HTH Ralf Mattes


> Is there any function to do this or must I go throw the entire
> list and adding a new scm_cons( new_elem , SCM_EOL ) at the end ?
> 
> Thanks
> -- 
> Laurent Marzullo <marzullo@la-defense.oilfield.slb.com>
> 
> 
> 
> _______________________________________________
> Guile-devel mailing list
> Guile-devel@gnu.org
> http://mail.gnu.org/mailman/listinfo/guile-devel


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: make an element last element of a list
  2003-10-21 15:50 ` rm
@ 2003-10-22 10:22   ` tomas
  2003-10-22 10:40     ` rm
  0 siblings, 1 reply; 5+ messages in thread
From: tomas @ 2003-10-22 10:22 UTC (permalink / raw)


On Tue, Oct 21, 2003 at 05:50:09PM +0200, rm@fabula.de wrote:
> On Tue, Oct 21, 2003 at 05:08:42PM +0200, Laurent Marzullo wrote:
> > Hello all,
> > 
> > I've got a scheme list 
> > 
> > SCM	scm_game_list;
> > 
> > and I want to add, with C API, an elements at the end of
> > the list.
> 
> If you want to destrutively modify your list you can do the
> following:
>  
>  (define my-lst (list 1 2 3 4))
>  (define new-last (list 5))
>  (set-cdr! (last my-lst) new-last)

...but you don't want to do this if the lists grow too large.
Keeping track of the last cons cell resp. maintaining a
reversed list (and reversing it once at the end, if ever
necessary) as another poster suggested is more efficient.
For short lists the solution is short and sweet, though.

Don't hesitate to ask if you need examples.

Cheers
-- tomas


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: make an element last element of a list
  2003-10-22 10:22   ` tomas
@ 2003-10-22 10:40     ` rm
  0 siblings, 0 replies; 5+ messages in thread
From: rm @ 2003-10-22 10:40 UTC (permalink / raw)
  Cc: guile-devel

> >  (define my-lst (list 1 2 3 4))
> >  (define new-last (list 5))
> >  (set-cdr! (last my-lst) new-last)
                ^^^^
Sorry, typo, should be (last-pair ....)

 RalfD

> ...but you don't want to do this if the lists grow too large.
> Keeping track of the last cons cell resp. maintaining a
> reversed list (and reversing it once at the end, if ever
> necessary) as another poster suggested is more efficient.
> For short lists the solution is short and sweet, though.
> 
> Don't hesitate to ask if you need examples.
> 
> Cheers
> -- tomas
> 
> 
> _______________________________________________
> Guile-devel mailing list
> Guile-devel@gnu.org
> http://mail.gnu.org/mailman/listinfo/guile-devel


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-10-22 10:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-10-21 15:08 make an element last element of a list Laurent Marzullo
2003-10-21 15:42 ` Stephen Compall
2003-10-21 15:50 ` rm
2003-10-22 10:22   ` tomas
2003-10-22 10:40     ` rm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).