guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating a nil cell


From: Dirk Herrmann
Subject: Re: Creating a nil cell
Date: Thu, 21 Sep 2000 16:04:30 +0200 (MEST)

On Thu, 21 Sep 2000, Lars J. Aas wrote:

> I've looked around a bit, but haven't found any other way to create a
> nil cell than to use gh_eval_str("'()").  It seems kind of excessive to
> have to use the evaluator for something as trivial as that.  What am
> I missing?

The macro SCM_EOL gives the object '().  However, it is not a `cell' in
that sense, it is just an object.  There are some other macros that might
be useful for you:

SCM_EOL - the empty list.
SCM_NULLP (x) - tests for the empty list.
SCM_CONSP (x) - tests whether the object is a pair (sorry for that name :-)
scm_ilength (x) - returns the length of the list, or a negative value if
                  the list is cyclic or does not form a proper list.

To scan through a list then can be efficiently done with the following
pattern, which is safe, even if `the_list' does not form a proper list
(except that it does not terminate if the list is cyclic):

for (l = the_list; SCM_CONSP (l); l = SCM_CDR (l))
  {
    /* Do whatever you want with the car of l */
  }
if (!SCM_NULLP (l))
  {
    /* the_list was not a proper list.  Maybe this is an error */
  }

If you may run into cyclic lists, you can use the following pattern.  The
core loop is faster, because it is known that we have a proper list.
However, you should know that scm_ilength has to perform a full scan of 
the list to determine the length, and also takes some measures to
determine cycles.  The time complexity is linear, though, with respect to
the length of the list, or, with respect to the number of pairs up to when
a cycle is reached.

if (scm_ilength (the_list) >= 0)
  {
    for (l = the_list; !SCM_NULLP (l); l = SCM_CDR (l))
      {
        /* Do whatever you want with the car of l */
      }
  }
else
  {
    /* the_list was not a proper list.  Maybe this is an error */
  }

Best regards
Dirk



reply via email to

[Prev in Thread] Current Thread [Next in Thread]