(scm_list): Restore this function for use from C.
[bpt/guile.git] / libguile / list.c
index f1552c5..a1a79a4 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995,1996,1997,2000,2001, 2003 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,2000,2001,2003,2004
+ * Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -12,7 +13,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 
 #include "libguile/list.h"
 #include "libguile/eval.h"
 
-#ifdef __STDC__
 #include <stdarg.h>
-#define var_start(x, y) va_start(x, y)
-#else
-#include <varargs.h>
-#define var_start(x, y) va_start(x)
-#endif
 
 \f
 /* creating lists */
@@ -87,29 +82,44 @@ scm_list_n (SCM elt, ...)
   SCM answer = SCM_EOL;
   SCM *pos = &answer;
 
-  var_start (foo, elt);
+  va_start (foo, elt);
   while (! SCM_UNBNDP (elt))
     {
+#if (SCM_DEBUG_CELL_ACCESSES == 1)
+      if (SCM_NIMP (elt))
+       SCM_VALIDATE_CELL(elt, 0);
+#endif      
       *pos = scm_cons (elt, SCM_EOL);
       pos = SCM_CDRLOC (*pos);
       elt = va_arg (foo, SCM);
     }
+  va_end (foo);
   return answer;
 }
 
 
-SCM_DEFINE (scm_list, "list", 0, 0, 1, 
-           (SCM objs),
-           "Return a list containing @var{objs}, the arguments to\n"
-           "@code{list}.")
-#define FUNC_NAME s_scm_list
+SCM_DEFINE (scm_make_list, "make-list", 1, 1, 0,
+            (SCM n, SCM init),
+           "Create a list containing of @var{n} elements, where each\n"
+           "element is initialized to @var{init}.  @var{init} defaults to\n"
+           "the empty list @code{()} if not given.")
+#define FUNC_NAME s_scm_make_list
 {
-  return objs;
+  unsigned nn = scm_to_uint (n);
+  unsigned i;
+  SCM ret = SCM_EOL;
+
+  if (SCM_UNBNDP (init))
+    init = SCM_EOL;
+
+  for (i = 0; i < nn; i++)
+    ret = scm_cons (init, ret);
+  return ret;
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1, 
+SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1,
             (SCM arg, SCM rest),
            "Like @code{list}, but the last arg provides the tail of the\n"
            "constructed list, returning @code{(cons @var{arg1} (cons\n"
@@ -119,18 +129,20 @@ SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1,
            "Schemes and in Common LISP.")
 #define FUNC_NAME s_scm_cons_star
 {
+  SCM ret = SCM_EOL;
+  SCM *p = &ret;
+
   SCM_VALIDATE_REST_ARGUMENT (rest);
-  if (!SCM_NULLP (rest))
+
+  for ( ; scm_is_pair (rest); rest = SCM_CDR (rest))
     {
-      SCM prev = arg = scm_cons (arg, rest);
-      while (!SCM_NULLP (SCM_CDR (rest)))
-       {
-         prev = rest;
-         rest = SCM_CDR (rest);
-       }
-      SCM_SETCDR (prev, SCM_CAR (rest));
+      *p = scm_cons (arg, SCM_EOL);
+      p = SCM_CDRLOC (*p);
+      arg = SCM_CAR (rest);
     }
-  return arg;
+
+  *p = arg;
+  return ret;
 }
 #undef FUNC_NAME
 
@@ -143,7 +155,7 @@ SCM_DEFINE (scm_null_p, "null?", 1, 0, 0,
            "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
 #define FUNC_NAME s_scm_null_p
 {
-  return SCM_BOOL (SCM_NULL_OR_NIL_P (x));
+  return scm_from_bool (SCM_NULL_OR_NIL_P (x));
 }
 #undef FUNC_NAME
 
@@ -153,7 +165,7 @@ SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
            "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
 #define FUNC_NAME s_scm_list_p
 {
-  return SCM_BOOL (scm_ilength (x) >= 0);
+  return scm_from_bool (scm_ilength (x) >= 0);
 }
 #undef FUNC_NAME
 
@@ -171,17 +183,17 @@ scm_ilength(SCM sx)
 
   do {
     if (SCM_NULL_OR_NIL_P(hare)) return i;
-    if (!SCM_CONSP (hare)) return -1;
+    if (!scm_is_pair (hare)) return -1;
     hare = SCM_CDR(hare);
     i++;
     if (SCM_NULL_OR_NIL_P(hare)) return i;
-    if (!SCM_CONSP (hare)) return -1;
+    if (!scm_is_pair (hare)) return -1;
     hare = SCM_CDR(hare);
     i++;
     /* For every two steps the hare takes, the tortoise takes one.  */
     tortoise = SCM_CDR(tortoise);
   }
-  while (! SCM_EQ_P (hare, tortoise));
+  while (!scm_is_eq (hare, tortoise));
 
   /* If the tortoise ever catches the hare, then the list must contain
      a cycle.  */
@@ -196,7 +208,7 @@ SCM_DEFINE (scm_length, "length", 1, 0, 0,
 {
   long i;
   SCM_VALIDATE_LIST_COPYLEN (1, lst, i);
-  return SCM_MAKINUM (i);
+  return scm_from_long (i);
 }
 #undef FUNC_NAME
 
@@ -224,22 +236,24 @@ SCM_DEFINE (scm_append, "append", 0, 0, 1,
 #define FUNC_NAME s_scm_append
 {
   SCM_VALIDATE_REST_ARGUMENT (args);
-  if (SCM_NULLP (args)) {
+  if (scm_is_null (args)) {
     return SCM_EOL;
   } else {
     SCM res = SCM_EOL;
     SCM *lloc = &res;
     SCM arg = SCM_CAR (args);
+    int argnum = 1;
     args = SCM_CDR (args);
-    while (!SCM_NULLP (args)) {
-      while (SCM_CONSP (arg)) {
+    while (!scm_is_null (args)) {
+      while (scm_is_pair (arg)) {
        *lloc = scm_cons (SCM_CAR (arg), SCM_EOL);
        lloc = SCM_CDRLOC (*lloc);
        arg = SCM_CDR (arg);
       }
-      SCM_VALIDATE_NULL_OR_NIL (SCM_ARGn, arg);
+      SCM_VALIDATE_NULL_OR_NIL (argnum, arg);
       arg = SCM_CAR (args);
       args = SCM_CDR (args);
+      argnum++;
     };
     *lloc = arg;
     return res;
@@ -253,33 +267,39 @@ SCM_DEFINE (scm_append_x, "append!", 0, 0, 1,
            "A destructive version of @code{append} (@pxref{Pairs and\n"
            "Lists,,,r5rs, The Revised^5 Report on Scheme}).  The cdr field\n"
            "of each list's final pair is changed to point to the head of\n"
-           "the next list, so no consing is performed.  Return a pointer to\n"
+           "the next list, so no consing is performed.  Return\n"
            "the mutated list.")
 #define FUNC_NAME s_scm_append_x
 {
+  SCM ret, *loc;
   SCM_VALIDATE_REST_ARGUMENT (lists);
-  while (1) {
-    if (SCM_NULLP (lists)) {
-      return SCM_EOL;
-    } else {
+
+  if (scm_is_null (lists))
+    return SCM_EOL;
+
+  loc = &ret;
+  for (;;)
+    {
       SCM arg = SCM_CAR (lists);
+      *loc = arg;
+
       lists = SCM_CDR (lists);
-      if (SCM_NULLP (lists)) {
-       return arg;
-      } else if (!SCM_NULL_OR_NIL_P (arg)) {
-       SCM_VALIDATE_CONS (SCM_ARG1, arg);
-       SCM_SETCDR (scm_last_pair (arg), scm_append_x (lists));
-       return arg;
-      }
+      if (scm_is_null (lists))
+        return ret;
+
+      if (!SCM_NULL_OR_NIL_P (arg))
+        {
+          SCM_VALIDATE_CONS (SCM_ARG1, arg);
+          loc = SCM_CDRLOC (scm_last_pair (arg));
+        }
     }
-  }
 }
 #undef FUNC_NAME
 
 
 SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0, 
            (SCM lst),
-           "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
+           "Return the last pair in @var{lst}, signalling an error if\n"
            "@var{lst} is circular.")
 #define FUNC_NAME s_scm_last_pair
 {
@@ -292,14 +312,14 @@ SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
   SCM_VALIDATE_CONS (SCM_ARG1, lst);
   do {
     SCM ahead = SCM_CDR(hare);
-    if (!SCM_CONSP (ahead)) return hare;
+    if (!scm_is_pair (ahead)) return hare;
     hare = ahead;
     ahead = SCM_CDR(hare);
-    if (!SCM_CONSP (ahead)) return hare;
+    if (!scm_is_pair (ahead)) return hare;
     hare = ahead;
     tortoise = SCM_CDR(tortoise);
   }
-  while (! SCM_EQ_P (hare, tortoise));
+  while (!scm_is_eq (hare, tortoise));
   SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
 }
 #undef FUNC_NAME
@@ -319,16 +339,16 @@ SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
 
   do {
       if (SCM_NULL_OR_NIL_P(hare)) return result;
-      SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
+      SCM_ASSERT(scm_is_pair(hare), lst, 1, FUNC_NAME);
       result = scm_cons (SCM_CAR (hare), result);
       hare = SCM_CDR (hare);
       if (SCM_NULL_OR_NIL_P(hare)) return result;
-      SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
+      SCM_ASSERT(scm_is_pair(hare), lst, 1, FUNC_NAME);
       result = scm_cons (SCM_CAR (hare), result);
       hare = SCM_CDR (hare);
       tortoise = SCM_CDR (tortoise);
     }
-  while (! SCM_EQ_P (hare, tortoise));
+  while (!scm_is_eq (hare, tortoise));
   SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
 }
 #undef FUNC_NAME
@@ -337,8 +357,8 @@ SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0,
             (SCM lst, SCM new_tail),
            "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
            "The Revised^5 Report on Scheme}).  The cdr of each cell in @var{lst} is\n"
-           "modified to point to the previous list element.  Return a pointer to the\n"
-           "head of the reversed list.\n\n"
+           "modified to point to the previous list element.  Return the\n"
+           "reversed list.\n\n"
            "Caveat: because the list is modified in place, the tail of the original\n"
            "list now becomes its head, and the head of the original list now becomes\n"
            "the tail.  Therefore, the @var{lst} symbol to which the head of the\n"
@@ -375,8 +395,8 @@ SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
 {
   SCM lst = list;
   unsigned long int i;
-  SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i);
-  while (SCM_CONSP (lst)) {
+  i = scm_to_ulong (k);
+  while (scm_is_pair (lst)) {
     if (i == 0)
       return SCM_CAR (lst);
     else {
@@ -398,9 +418,8 @@ SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
 #define FUNC_NAME s_scm_list_set_x
 {
   SCM lst = list;
-  unsigned long int i;
-  SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i);
-  while (SCM_CONSP (lst)) {
+  unsigned long int i = scm_to_ulong (k);
+  while (scm_is_pair (lst)) {
     if (i == 0) {
       SCM_SETCAR (lst, val);
       return val;
@@ -429,8 +448,7 @@ SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
            "or returning the results of cdring @var{k} times down @var{lst}.")
 #define FUNC_NAME s_scm_list_tail
 {
-  register long i;
-  SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i);
+  size_t i = scm_to_size_t (k);
   while (i-- > 0) {
     SCM_VALIDATE_CONS (1, lst);
     lst = SCM_CDR(lst);
@@ -446,9 +464,8 @@ SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
 #define FUNC_NAME s_scm_list_cdr_set_x
 {
   SCM lst = list;
-  unsigned long int i;
-  SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i);
-  while (SCM_CONSP (lst)) {
+  size_t i = scm_to_size_t (k);
+  while (scm_is_pair (lst)) {
     if (i == 0) {
       SCM_SETCDR (lst, val);
       return val;
@@ -476,9 +493,8 @@ SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
 {
   SCM answer;
   SCM * pos;
-  register long i;
+  size_t i = scm_to_size_t (k);
 
-  SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i);
   answer = SCM_EOL;
   pos = &answer;
   while (i-- > 0)
@@ -493,6 +509,34 @@ SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
 #undef FUNC_NAME
 
 
+/* Copy a list which is known to be finite.  The last pair may or may not have
+ * a '() in its cdr.  That is, improper lists are accepted.  */
+SCM
+scm_i_finite_list_copy (SCM list)
+{
+  if (!scm_is_pair (list))
+    {
+      return list;
+    }
+  else
+    {
+      SCM tail;
+      const SCM result = tail = scm_list_1 (SCM_CAR (list));
+      list = SCM_CDR (list);
+      while (scm_is_pair (list))
+        {
+          const SCM new_tail = scm_list_1 (SCM_CAR (list));
+          SCM_SETCDR (tail, new_tail);
+          tail = new_tail;
+          list = SCM_CDR (list);
+        }
+      SCM_SETCDR (tail, list);
+
+      return result;
+    }
+}
+
+
 SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0, 
             (SCM lst),
            "Return a (newly-created) copy of @var{lst}.")
@@ -508,7 +552,7 @@ SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
   fill_here = &newlst;
   from_here = lst;
 
-  while (SCM_CONSP (from_here))
+  while (scm_is_pair (from_here))
     {
       SCM c;
       c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
@@ -520,6 +564,23 @@ SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
 }
 #undef FUNC_NAME
 
+
+SCM_PROC (s_list, "list", 0, 0, 1, scm_list_copy);
+SCM_SNARF_DOCS (primitive, scm_list_copy, "list", (SCM objs), 0, 0, 1,
+                "Return a list containing @var{objs}, the arguments to\n"
+                "@code{list}.")
+
+/* This used to be the code for "list", but it's wrong when used via apply
+   (it should copy the list).  It seems pretty unlikely anyone would have
+   been using this from C code, since it's a no-op, but keep it for strict
+   binary compatibility.  */
+SCM
+scm_list (SCM objs)
+{
+  return objs;
+}
+
+
 \f
 /* membership tests (memq, memv, etc.) */ 
 
@@ -535,7 +596,7 @@ scm_c_memq (SCM obj, SCM list)
 {
   for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR (list))
     {
-      if (SCM_EQ_P (SCM_CAR (list), obj))
+      if (scm_is_eq (SCM_CAR (list), obj))
        return list;
     }
   return SCM_BOOL_F;
@@ -571,7 +632,7 @@ SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
   SCM_VALIDATE_LIST (2, lst);
   for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
     {
-      if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
+      if (! scm_is_false (scm_eqv_p (SCM_CAR (lst), x)))
        return lst;
     }
   return SCM_BOOL_F;
@@ -592,7 +653,7 @@ SCM_DEFINE (scm_member, "member", 2, 0, 0,
   SCM_VALIDATE_LIST (2, lst);
   for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
     {
-      if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
+      if (! scm_is_false (scm_equal_p (SCM_CAR (lst), x)))
        return lst;
     }
   return SCM_BOOL_F;
@@ -607,7 +668,7 @@ SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
            "@deffnx {Scheme Procedure} delv! item lst\n"
            "@deffnx {Scheme Procedure} delete! item lst\n"
            "These procedures are destructive versions of @code{delq}, @code{delv}\n"
-           "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
+           "and @code{delete}: they modify the existing @var{lst}\n"
            "rather than creating a new list.  Caveat evaluator: Like other\n"
            "destructive list functions, these functions cannot modify the binding of\n"
            "@var{lst}, and so cannot be used to delete the first element of\n"
@@ -618,10 +679,10 @@ SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (SCM_EQ_P (SCM_CAR (walk), item))
+      if (scm_is_eq (SCM_CAR (walk), item))
        *prev = SCM_CDR (walk);
       else
        prev = SCM_CDRLOC (walk);
@@ -642,10 +703,10 @@ SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
+      if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
        *prev = SCM_CDR (walk);
       else
        prev = SCM_CDRLOC (walk);
@@ -667,10 +728,10 @@ SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
+      if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
        *prev = SCM_CDR (walk);
       else
        prev = SCM_CDRLOC (walk);
@@ -735,10 +796,10 @@ SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (SCM_EQ_P (SCM_CAR (walk), item))
+      if (scm_is_eq (SCM_CAR (walk), item))
        {
          *prev = SCM_CDR (walk);
          break;
@@ -763,10 +824,10 @@ SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
+      if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
        {
          *prev = SCM_CDR (walk);
          break;
@@ -791,10 +852,10 @@ SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
   SCM *prev;
 
   for (prev = &lst, walk = lst;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
+      if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
        {
          *prev = SCM_CDR (walk);
          break;
@@ -827,10 +888,10 @@ SCM_DEFINE (scm_filter, "filter", 2, 0, 0,
   SCM_VALIDATE_LIST (2, list);
   
   for (prev = &res, walk = list;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (!SCM_FALSEP (call (pred, SCM_CAR (walk))))
+      if (scm_is_true (call (pred, SCM_CAR (walk))))
        {
          *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
          prev = SCM_CDRLOC (*prev);
@@ -853,10 +914,10 @@ SCM_DEFINE (scm_filter_x, "filter!", 2, 0, 0,
   SCM_VALIDATE_LIST (2, list);
   
   for (prev = &list, walk = list;
-       SCM_CONSP (walk);
+       scm_is_pair (walk);
        walk = SCM_CDR (walk))
     {
-      if (!SCM_FALSEP (call (pred, SCM_CAR (walk))))
+      if (scm_is_true (call (pred, SCM_CAR (walk))))
        prev = SCM_CDRLOC (walk);
       else
        *prev = SCM_CDR (walk);