2002-12-01 13:10:51 +00:00
|
|
|
|
/* srfi-1.c --- SRFI-1 procedures for Guile
|
|
|
|
|
|
*
|
2014-09-30 03:50:47 -04:00
|
|
|
|
* Copyright (C) 1995-1997, 2000-2003, 2005, 2006, 2008-2011, 2013
|
2014-04-18 15:04:12 -04:00
|
|
|
|
* 2014 Free Software Foundation, Inc.
|
2006-02-04 00:50:39 +00:00
|
|
|
|
*
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
|
|
* as published by the Free Software Foundation; either version 3 of
|
|
|
|
|
|
* the License, or (at your option) any later version.
|
2002-12-01 13:10:51 +00:00
|
|
|
|
*
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
* Lesser General Public License for more details.
|
2002-12-01 13:10:51 +00:00
|
|
|
|
*
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
|
* 02110-1301 USA
|
2003-04-05 19:15:35 +00:00
|
|
|
|
*/
|
2002-12-01 13:10:51 +00:00
|
|
|
|
|
2010-09-12 23:18:54 +02:00
|
|
|
|
|
2008-09-13 15:35:27 +02:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2008-04-27 00:50:05 +02:00
|
|
|
|
# include <config.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2010-09-12 23:18:54 +02:00
|
|
|
|
#include "libguile/_scm.h"
|
|
|
|
|
|
#include "libguile/eq.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "libguile/validate.h"
|
|
|
|
|
|
#include "libguile/list.h"
|
|
|
|
|
|
#include "libguile/eval.h"
|
|
|
|
|
|
#include "libguile/srfi-1.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2002-12-01 13:10:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
Start rewriting SRFI-1 in Scheme.
This partially reverts commit e556f8c3c6b74ee6596e8dcbe829109d7745da2c
(Fri May 6 2005).
* module/srfi/srfi-1.scm (xcons, list-tabulate, not-pair?, car+cdr,
last, fold, list-index): New procedures.
* srfi/srfi-1.c (srfi1_module): New variable.
(CACHE_VAR): New macro.
(scm_srfi1_car_plus_cdr, scm_srfi1_fold, scm_srfi1_last,
scm_srfi1_list_index, scm_srfi1_list_tabulate, scm_srfi1_not_pair_p,
scm_srfi1_xcons): Rewrite as proxies of the corresponding Scheme
procedure.
* test-suite/tests/srfi-1.test ("list-tabulate")["-1"]: Change exception
type to `exception:wrong-type-arg'.
* benchmark-suite/benchmarks/srfi-1.bm: New file.
* benchmark-suite/Makefile.am (SCM_BENCHMARKS): Add
`benchmarks/srfi-1.bm'.
* test-suite/standalone/Makefile.am (test_srfi_1_SOURCES,
test_srfi_1_CFLAGS, test_srfi_1_LDADD): New variables.
(check_PROGRAMS): Add `test-srfi-1'.
(TESTS): Ditto.
* test-suite/standalone/test-srfi-1.c: New file.
2010-07-13 00:07:12 +02:00
|
|
|
|
/* The intent of this file was to gradually replace those Scheme
|
|
|
|
|
|
* procedures in srfi-1.scm that extend core primitive procedures,
|
2010-09-12 23:18:54 +02:00
|
|
|
|
* so that using srfi-1 wouldn't have performance penalties.
|
2002-12-01 13:10:51 +00:00
|
|
|
|
*
|
Start rewriting SRFI-1 in Scheme.
This partially reverts commit e556f8c3c6b74ee6596e8dcbe829109d7745da2c
(Fri May 6 2005).
* module/srfi/srfi-1.scm (xcons, list-tabulate, not-pair?, car+cdr,
last, fold, list-index): New procedures.
* srfi/srfi-1.c (srfi1_module): New variable.
(CACHE_VAR): New macro.
(scm_srfi1_car_plus_cdr, scm_srfi1_fold, scm_srfi1_last,
scm_srfi1_list_index, scm_srfi1_list_tabulate, scm_srfi1_not_pair_p,
scm_srfi1_xcons): Rewrite as proxies of the corresponding Scheme
procedure.
* test-suite/tests/srfi-1.test ("list-tabulate")["-1"]: Change exception
type to `exception:wrong-type-arg'.
* benchmark-suite/benchmarks/srfi-1.bm: New file.
* benchmark-suite/Makefile.am (SCM_BENCHMARKS): Add
`benchmarks/srfi-1.bm'.
* test-suite/standalone/Makefile.am (test_srfi_1_SOURCES,
test_srfi_1_CFLAGS, test_srfi_1_LDADD): New variables.
(check_PROGRAMS): Add `test-srfi-1'.
(TESTS): Ditto.
* test-suite/standalone/test-srfi-1.c: New file.
2010-07-13 00:07:12 +02:00
|
|
|
|
* However, we now prefer to write these procedures in Scheme, let the compiler
|
|
|
|
|
|
* optimize them, and have the VM execute them efficiently.
|
2002-12-01 13:10:51 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
Start rewriting SRFI-1 in Scheme.
This partially reverts commit e556f8c3c6b74ee6596e8dcbe829109d7745da2c
(Fri May 6 2005).
* module/srfi/srfi-1.scm (xcons, list-tabulate, not-pair?, car+cdr,
last, fold, list-index): New procedures.
* srfi/srfi-1.c (srfi1_module): New variable.
(CACHE_VAR): New macro.
(scm_srfi1_car_plus_cdr, scm_srfi1_fold, scm_srfi1_last,
scm_srfi1_list_index, scm_srfi1_list_tabulate, scm_srfi1_not_pair_p,
scm_srfi1_xcons): Rewrite as proxies of the corresponding Scheme
procedure.
* test-suite/tests/srfi-1.test ("list-tabulate")["-1"]: Change exception
type to `exception:wrong-type-arg'.
* benchmark-suite/benchmarks/srfi-1.bm: New file.
* benchmark-suite/Makefile.am (SCM_BENCHMARKS): Add
`benchmarks/srfi-1.bm'.
* test-suite/standalone/Makefile.am (test_srfi_1_SOURCES,
test_srfi_1_CFLAGS, test_srfi_1_LDADD): New variables.
(check_PROGRAMS): Add `test-srfi-1'.
(TESTS): Ditto.
* test-suite/standalone/test-srfi-1.c: New file.
2010-07-13 00:07:12 +02:00
|
|
|
|
|
2003-07-08 00:09:17 +00:00
|
|
|
|
static SCM
|
|
|
|
|
|
equal_trampoline (SCM proc, SCM arg1, SCM arg2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return scm_equal_p (arg1, arg2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* list_copy_part() copies the first COUNT cells of LST, puts the result at
|
|
|
|
|
|
*dst, and returns the SCM_CDRLOC of the last cell in that new list.
|
|
|
|
|
|
|
|
|
|
|
|
This function is designed to be careful about LST possibly having changed
|
|
|
|
|
|
in between the caller deciding what to copy, and the copy actually being
|
|
|
|
|
|
done here. The COUNT ensures we terminate if LST has become circular,
|
|
|
|
|
|
SCM_VALIDATE_CONS guards against a cdr in the list changed to some
|
|
|
|
|
|
non-pair object. */
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
static SCM *
|
|
|
|
|
|
list_copy_part (SCM lst, int count, SCM *dst)
|
|
|
|
|
|
#define FUNC_NAME "list_copy_part"
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM c;
|
|
|
|
|
|
for ( ; count > 0; count--)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_CONS (SCM_ARGn, lst);
|
|
|
|
|
|
c = scm_cons (SCM_CAR (lst), SCM_EOL);
|
|
|
|
|
|
*dst = c;
|
|
|
|
|
|
dst = SCM_CDRLOC (c);
|
|
|
|
|
|
lst = SCM_CDR (lst);
|
|
|
|
|
|
}
|
|
|
|
|
|
return dst;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
2006-06-17 22:47:50 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_append_reverse, "append-reverse", 2, 0, 0,
|
|
|
|
|
|
(SCM revhead, SCM tail),
|
|
|
|
|
|
"Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
|
|
|
|
|
|
"result. This is equivalent to @code{(append (reverse\n"
|
|
|
|
|
|
"@var{rev-head}) @var{tail})}, but its implementation is more\n"
|
|
|
|
|
|
"efficient.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@example\n"
|
|
|
|
|
|
"(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
|
|
|
|
|
|
"@end example")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_append_reverse
|
|
|
|
|
|
{
|
|
|
|
|
|
while (scm_is_pair (revhead))
|
|
|
|
|
|
{
|
|
|
|
|
|
/* copy first element of revhead onto front of tail */
|
|
|
|
|
|
tail = scm_cons (SCM_CAR (revhead), tail);
|
|
|
|
|
|
revhead = SCM_CDR (revhead);
|
|
|
|
|
|
}
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
|
|
|
|
|
|
"list");
|
|
|
|
|
|
return tail;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_append_reverse_x, "append-reverse!", 2, 0, 0,
|
|
|
|
|
|
(SCM revhead, SCM tail),
|
|
|
|
|
|
"Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
|
|
|
|
|
|
"result. This is equivalent to @code{(append! (reverse!\n"
|
|
|
|
|
|
"@var{rev-head}) @var{tail})}, but its implementation is more\n"
|
|
|
|
|
|
"efficient.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@example\n"
|
|
|
|
|
|
"(append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
|
|
|
|
|
|
"@end example\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{rev-head} may be modified in order to produce the result.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_append_reverse_x
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM newtail;
|
|
|
|
|
|
|
Attempt to mutate residualized literal pair throws exception
* libguile/validate.h (SCM_VALIDATE_MUTABLE_PAIR):
* libguile/pairs.h (scm_is_mutable_pair): New internal definitions.
* libguile/pairs.c (scm_set_car_x, scm_set_cdr_x): Validate mutable
pairs.
* libguile/alist.c (scm_assq_set_x, scm_assv_set_x, scm_assoc_set_x):
* libguile/list.c (scm_reverse_x, scm_list_set_x, scm_list_cdr_set_x):
* libguile/srcprop.c (scm_make_srcprops):
* libguile/srfi-1.c (scm_srfi1_append_reverse_x)
(scm_srfi1_delete_duplicates_x):
* libguile/symbols.c (scm_symbol_fset_x, scm_symbol_pset_x):
* libguile/sort.c (scm_merge_list_x): Use scm_set_car_x / scm_set_cdr_x
instead of the macros, so as to check for mutable pairs.
(SCM_VALIDATE_MUTABLE_LIST): New internal helper macro.
(scm_sort_x, scm_stable_sort_x, scm_sort_list_x): Use
SCM_VALIDATE_MUTABLE_LIST.
* libguile/vm-engine.c (VM_VALIDATE_MUTABLE_PAIR): New definition.
(set-car!, set-cdr!): Use VM_VALIDATE_MUTABLE_PAIR. Fix error message
for set-cdr!.
2017-04-17 11:26:17 +02:00
|
|
|
|
while (scm_is_mutable_pair (revhead))
|
2006-06-17 22:47:50 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* take the first cons cell from revhead */
|
|
|
|
|
|
newtail = revhead;
|
|
|
|
|
|
revhead = SCM_CDR (revhead);
|
|
|
|
|
|
|
|
|
|
|
|
/* make it the new start of tail, appending the previous */
|
|
|
|
|
|
SCM_SETCDR (newtail, tail);
|
|
|
|
|
|
tail = newtail;
|
|
|
|
|
|
}
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
|
|
|
|
|
|
"list");
|
|
|
|
|
|
return tail;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2005-04-03 22:41:15 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
|
|
|
|
|
|
(SCM lstlst),
|
|
|
|
|
|
"Construct a list by appending all lists in @var{lstlst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@code{concatenate} is the same as @code{(apply append\n"
|
|
|
|
|
|
"@var{lstlst})}. It exists because some Scheme implementations\n"
|
|
|
|
|
|
"have a limit on the number of arguments a function takes, which\n"
|
|
|
|
|
|
"the @code{apply} might exceed. In Guile there is no such\n"
|
|
|
|
|
|
"limit.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_concatenate
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
|
|
|
|
|
|
return scm_append (lstlst);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2003-07-28 23:43:51 +00:00
|
|
|
|
|
2005-04-03 22:41:15 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
|
|
|
|
|
|
(SCM lstlst),
|
|
|
|
|
|
"Construct a list by appending all lists in @var{lstlst}. Those\n"
|
|
|
|
|
|
"lists may be modified to produce the result.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@code{concatenate!} is the same as @code{(apply append!\n"
|
|
|
|
|
|
"@var{lstlst})}. It exists because some Scheme implementations\n"
|
|
|
|
|
|
"have a limit on the number of arguments a function takes, which\n"
|
|
|
|
|
|
"the @code{apply} might exceed. In Guile there is no such\n"
|
|
|
|
|
|
"limit.")
|
2011-02-02 15:12:26 +01:00
|
|
|
|
#define FUNC_NAME s_scm_srfi1_concatenate_x
|
2005-04-03 22:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
|
|
|
|
|
|
return scm_append_x (lstlst);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
2003-07-28 23:43:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
2003-12-02 21:12:20 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
|
2004-09-03 19:45:37 +00:00
|
|
|
|
(SCM pred, SCM list1, SCM rest),
|
2003-12-02 21:12:20 +00:00
|
|
|
|
"Return a count of the number of times @var{pred} returns true\n"
|
|
|
|
|
|
"when called on elements from the given lists.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
|
|
|
|
|
|
"@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
|
2004-09-03 19:45:37 +00:00
|
|
|
|
"corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
|
2003-12-02 21:12:20 +00:00
|
|
|
|
"with the first element of each list, the second with the second\n"
|
|
|
|
|
|
"element from each, and so on.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Counting stops when the end of the shortest list is reached.\n"
|
|
|
|
|
|
"At least one list must be non-circular.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_count
|
|
|
|
|
|
{
|
|
|
|
|
|
long count;
|
2005-04-01 23:43:49 +00:00
|
|
|
|
SCM lst;
|
|
|
|
|
|
int argnum;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
SCM_VALIDATE_REST_ARGUMENT (rest);
|
|
|
|
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (scm_is_null (rest))
|
2003-12-02 21:12:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* one list */
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2005-03-02 20:50:48 +00:00
|
|
|
|
for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
|
2009-11-27 20:36:09 +01:00
|
|
|
|
count += scm_is_true (scm_call_1 (pred, SCM_CAR (list1)));
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2005-04-01 23:43:49 +00:00
|
|
|
|
/* check below that list1 is a proper list, and done */
|
|
|
|
|
|
end_list1:
|
|
|
|
|
|
lst = list1;
|
|
|
|
|
|
argnum = 2;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
}
|
2005-03-02 20:50:48 +00:00
|
|
|
|
else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
|
2003-12-02 21:12:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* two lists */
|
2004-09-03 19:45:37 +00:00
|
|
|
|
SCM list2;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2004-09-03 19:45:37 +00:00
|
|
|
|
list2 = SCM_CAR (rest);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (! scm_is_pair (list1))
|
2005-04-01 23:43:49 +00:00
|
|
|
|
goto end_list1;
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (! scm_is_pair (list2))
|
2003-12-02 21:12:20 +00:00
|
|
|
|
{
|
2005-04-01 23:43:49 +00:00
|
|
|
|
lst = list2;
|
|
|
|
|
|
argnum = 3;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2009-11-27 20:36:09 +01:00
|
|
|
|
count += scm_is_true (scm_call_2
|
2004-09-03 19:45:37 +00:00
|
|
|
|
(pred, SCM_CAR (list1), SCM_CAR (list2)));
|
|
|
|
|
|
list1 = SCM_CDR (list1);
|
|
|
|
|
|
list2 = SCM_CDR (list2);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* three or more lists */
|
2005-04-23 00:10:50 +00:00
|
|
|
|
SCM vec, args, a;
|
|
|
|
|
|
size_t len, i;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2005-04-23 00:10:50 +00:00
|
|
|
|
/* vec is the list arguments */
|
|
|
|
|
|
vec = scm_vector (scm_cons (list1, rest));
|
|
|
|
|
|
len = SCM_SIMPLE_VECTOR_LENGTH (vec);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
2005-04-23 00:10:50 +00:00
|
|
|
|
/* args is the argument list to pass to pred, same length as vec,
|
2003-12-02 21:12:20 +00:00
|
|
|
|
re-used for each call */
|
2005-04-23 00:10:50 +00:00
|
|
|
|
args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
2005-04-23 00:10:50 +00:00
|
|
|
|
/* first elem of each list in vec into args, and step those
|
|
|
|
|
|
vec entries onto their next element */
|
|
|
|
|
|
for (i = 0, a = args, argnum = 2;
|
|
|
|
|
|
i < len;
|
|
|
|
|
|
i++, a = SCM_CDR (a), argnum++)
|
2003-12-02 21:12:20 +00:00
|
|
|
|
{
|
2005-04-23 00:10:50 +00:00
|
|
|
|
lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (! scm_is_pair (lst))
|
2005-04-01 23:43:49 +00:00
|
|
|
|
goto check_lst_and_done;
|
2003-12-02 21:12:20 +00:00
|
|
|
|
SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
|
2005-04-23 00:10:50 +00:00
|
|
|
|
SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
|
2003-12-02 21:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-21 15:41:27 +01:00
|
|
|
|
count += scm_is_true (scm_apply_0 (pred, args));
|
2003-12-02 21:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2005-04-01 23:43:49 +00:00
|
|
|
|
|
|
|
|
|
|
check_lst_and_done:
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
|
2004-07-08 15:58:11 +00:00
|
|
|
|
return scm_from_long (count);
|
2003-12-02 21:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-07-08 00:09:17 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
|
|
|
|
|
|
(SCM x, SCM lst, SCM pred),
|
|
|
|
|
|
"Return a list containing the elements of @var{lst} but with\n"
|
|
|
|
|
|
"those equal to @var{x} deleted. The returned elements will be\n"
|
|
|
|
|
|
"in the same order as they were in @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Equality is determined by @var{pred}, or @code{equal?} if not\n"
|
|
|
|
|
|
"given. An equality call is made just once for each element,\n"
|
|
|
|
|
|
"but the order in which the calls are made on the elements is\n"
|
|
|
|
|
|
"unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The equality calls are always @code{(pred x elem)}, ie.@: the\n"
|
|
|
|
|
|
"given @var{x} is first. This means for instance elements\n"
|
|
|
|
|
|
"greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} is not modified, but the returned list might share a\n"
|
|
|
|
|
|
"common tail with @var{lst}.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_delete
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM ret, *p, keeplst;
|
2006-02-04 00:50:39 +00:00
|
|
|
|
int count;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
|
|
|
|
|
if (SCM_UNBNDP (pred))
|
|
|
|
|
|
return scm_delete (x, lst);
|
|
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
|
|
|
|
|
/* ret is the return list being constructed. p is where to append to it,
|
|
|
|
|
|
initially &ret then SCM_CDRLOC of the last pair. lst progresses as
|
|
|
|
|
|
elements are considered.
|
|
|
|
|
|
|
|
|
|
|
|
Elements to be retained are not immediately copied, instead keeplst is
|
2006-02-04 00:50:39 +00:00
|
|
|
|
the last pair in lst which is to be retained but not yet copied, count
|
|
|
|
|
|
is how many from there are wanted. When there's no more deletions, *p
|
|
|
|
|
|
can be set to keeplst to share the remainder of the original lst. (The
|
|
|
|
|
|
entire original lst if there's no deletions at all.) */
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
|
|
|
|
|
keeplst = lst;
|
2006-02-04 00:50:39 +00:00
|
|
|
|
count = 0;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
p = &ret;
|
|
|
|
|
|
|
2005-03-02 20:50:48 +00:00
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (lst))))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* delete this element, so copy those at keeplst */
|
|
|
|
|
|
p = list_copy_part (keeplst, count, p);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
keeplst = SCM_CDR (lst);
|
2006-02-04 00:50:39 +00:00
|
|
|
|
count = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* keep this element */
|
|
|
|
|
|
count++;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* final retained elements */
|
|
|
|
|
|
*p = keeplst;
|
|
|
|
|
|
|
|
|
|
|
|
/* demand that lst was a proper list */
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
|
|
|
|
|
|
(SCM x, SCM lst, SCM pred),
|
|
|
|
|
|
"Return a list containing the elements of @var{lst} but with\n"
|
|
|
|
|
|
"those equal to @var{x} deleted. The returned elements will be\n"
|
|
|
|
|
|
"in the same order as they were in @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Equality is determined by @var{pred}, or @code{equal?} if not\n"
|
|
|
|
|
|
"given. An equality call is made just once for each element,\n"
|
|
|
|
|
|
"but the order in which the calls are made on the elements is\n"
|
|
|
|
|
|
"unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The equality calls are always @code{(pred x elem)}, ie.@: the\n"
|
|
|
|
|
|
"given @var{x} is first. This means for instance elements\n"
|
|
|
|
|
|
"greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} may be modified to construct the returned list.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_delete_x
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM walk;
|
|
|
|
|
|
SCM *prev;
|
|
|
|
|
|
|
|
|
|
|
|
if (SCM_UNBNDP (pred))
|
|
|
|
|
|
return scm_delete_x (x, lst);
|
|
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
|
|
|
|
|
for (prev = &lst, walk = lst;
|
2005-03-02 20:50:48 +00:00
|
|
|
|
scm_is_pair (walk);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
walk = SCM_CDR (walk))
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (walk))))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
*prev = SCM_CDR (walk);
|
|
|
|
|
|
else
|
|
|
|
|
|
prev = SCM_CDRLOC (walk);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* demand the input was a proper list */
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
|
|
|
|
|
|
return lst;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
|
|
|
|
|
|
(SCM lst, SCM pred),
|
|
|
|
|
|
"Return a list containing the elements of @var{lst} but without\n"
|
|
|
|
|
|
"duplicates.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"When elements are equal, only the first in @var{lst} is\n"
|
|
|
|
|
|
"retained. Equal elements can be anywhere in @var{lst}, they\n"
|
|
|
|
|
|
"don't have to be adjacent. The returned list will have the\n"
|
|
|
|
|
|
"retained elements in the same order as they were in @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Equality is determined by @var{pred}, or @code{equal?} if not\n"
|
|
|
|
|
|
"given. Calls @code{(pred x y)} are made with element @var{x}\n"
|
|
|
|
|
|
"being before @var{y} in @var{lst}. A call is made at most once\n"
|
|
|
|
|
|
"for each combination, but the sequence of the calls across the\n"
|
|
|
|
|
|
"elements is unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} is not modified, but the return might share a common\n"
|
|
|
|
|
|
"tail with @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"In the worst case, this is an @math{O(N^2)} algorithm because\n"
|
|
|
|
|
|
"it must check each element against all those preceding it. For\n"
|
|
|
|
|
|
"long lists it is more efficient to sort and then compare only\n"
|
|
|
|
|
|
"adjacent elements.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_delete_duplicates
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_t_trampoline_2 equal_p;
|
|
|
|
|
|
SCM ret, *p, keeplst, item, l;
|
2006-02-04 00:50:39 +00:00
|
|
|
|
int count, i;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
|
|
|
|
|
/* ret is the new list constructed. p is where to append, initially &ret
|
|
|
|
|
|
then SCM_CDRLOC of the last pair. lst is advanced as each element is
|
|
|
|
|
|
considered.
|
|
|
|
|
|
|
|
|
|
|
|
Elements retained are not immediately appended to ret, instead keeplst
|
|
|
|
|
|
is the last pair in lst which is to be kept but is not yet copied.
|
|
|
|
|
|
Initially this is the first pair of lst, since the first element is
|
|
|
|
|
|
always retained.
|
|
|
|
|
|
|
|
|
|
|
|
*p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
|
|
|
|
|
|
the elements retained, making the equality search loop easy.
|
|
|
|
|
|
|
|
|
|
|
|
If an item must be deleted, elements from keeplst (inclusive) to lst
|
|
|
|
|
|
(exclusive) must be copied and appended to ret. When there's no more
|
|
|
|
|
|
deletions, *p is left set to keeplst, so ret shares structure with the
|
|
|
|
|
|
original lst. (ret will be the entire original lst if there are no
|
|
|
|
|
|
deletions.) */
|
|
|
|
|
|
|
|
|
|
|
|
/* skip to end if an empty list (or something invalid) */
|
2006-02-04 00:50:39 +00:00
|
|
|
|
ret = SCM_EOL;
|
|
|
|
|
|
|
|
|
|
|
|
if (SCM_UNBNDP (pred))
|
|
|
|
|
|
equal_p = equal_trampoline;
|
|
|
|
|
|
else
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG2, pred);
|
|
|
|
|
|
equal_p = scm_call_2;
|
2006-02-04 00:50:39 +00:00
|
|
|
|
}
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
keeplst = lst;
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
p = &ret;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
|
|
|
|
|
{
|
|
|
|
|
|
item = SCM_CAR (lst);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* look for item in "ret" list */
|
|
|
|
|
|
for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* "item" is a duplicate, so copy keeplst onto ret */
|
|
|
|
|
|
duplicate:
|
|
|
|
|
|
p = list_copy_part (keeplst, count, p);
|
|
|
|
|
|
|
|
|
|
|
|
keeplst = SCM_CDR (lst); /* elem after the one deleted */
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
goto next_elem;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* look for item in "keeplst" list
|
|
|
|
|
|
be careful traversing, in case nasty code changed the cdrs */
|
|
|
|
|
|
for (i = 0, l = keeplst;
|
|
|
|
|
|
i < count && scm_is_pair (l);
|
|
|
|
|
|
i++, l = SCM_CDR (l))
|
|
|
|
|
|
if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
|
|
|
|
|
|
goto duplicate;
|
|
|
|
|
|
|
|
|
|
|
|
/* keep this element */
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
|
|
next_elem:
|
|
|
|
|
|
;
|
|
|
|
|
|
}
|
2003-07-08 00:09:17 +00:00
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
|
|
|
|
|
|
|
2006-02-04 00:50:39 +00:00
|
|
|
|
/* share tail of keeplst items */
|
|
|
|
|
|
*p = keeplst;
|
|
|
|
|
|
|
2003-07-08 00:09:17 +00:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
|
|
|
|
|
|
(SCM lst, SCM pred),
|
|
|
|
|
|
"Return a list containing the elements of @var{lst} but without\n"
|
|
|
|
|
|
"duplicates.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"When elements are equal, only the first in @var{lst} is\n"
|
|
|
|
|
|
"retained. Equal elements can be anywhere in @var{lst}, they\n"
|
|
|
|
|
|
"don't have to be adjacent. The returned list will have the\n"
|
|
|
|
|
|
"retained elements in the same order as they were in @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Equality is determined by @var{pred}, or @code{equal?} if not\n"
|
|
|
|
|
|
"given. Calls @code{(pred x y)} are made with element @var{x}\n"
|
|
|
|
|
|
"being before @var{y} in @var{lst}. A call is made at most once\n"
|
|
|
|
|
|
"for each combination, but the sequence of the calls across the\n"
|
|
|
|
|
|
"elements is unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} may be modified to construct the returned list.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"In the worst case, this is an @math{O(N^2)} algorithm because\n"
|
|
|
|
|
|
"it must check each element against all those preceding it. For\n"
|
|
|
|
|
|
"long lists it is more efficient to sort and then compare only\n"
|
|
|
|
|
|
"adjacent elements.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_delete_duplicates_x
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_t_trampoline_2 equal_p;
|
|
|
|
|
|
SCM ret, endret, item, l;
|
|
|
|
|
|
|
|
|
|
|
|
/* ret is the return list, constructed from the pairs in lst. endret is
|
|
|
|
|
|
the last pair of ret, initially the first pair. lst is advanced as
|
|
|
|
|
|
elements are considered. */
|
|
|
|
|
|
|
|
|
|
|
|
/* skip to end if an empty list (or something invalid) */
|
|
|
|
|
|
ret = lst;
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (scm_is_pair (lst))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (pred))
|
|
|
|
|
|
equal_p = equal_trampoline;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG2, pred);
|
|
|
|
|
|
equal_p = scm_call_2;
|
2003-07-08 00:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
endret = ret;
|
|
|
|
|
|
|
|
|
|
|
|
/* loop over lst elements starting from second */
|
|
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
|
|
|
|
|
lst = SCM_CDR (lst);
|
2005-03-02 20:50:48 +00:00
|
|
|
|
if (! scm_is_pair (lst))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
break;
|
|
|
|
|
|
item = SCM_CAR (lst);
|
|
|
|
|
|
|
|
|
|
|
|
/* is item equal to any element from ret to endret (inclusive)? */
|
|
|
|
|
|
l = ret;
|
|
|
|
|
|
for (;;)
|
|
|
|
|
|
{
|
2004-07-06 11:02:47 +00:00
|
|
|
|
if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
break; /* equal, forget this element */
|
|
|
|
|
|
|
2004-07-27 15:41:49 +00:00
|
|
|
|
if (scm_is_eq (l, endret))
|
2003-07-08 00:09:17 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* not equal to any, so append this pair */
|
Attempt to mutate residualized literal pair throws exception
* libguile/validate.h (SCM_VALIDATE_MUTABLE_PAIR):
* libguile/pairs.h (scm_is_mutable_pair): New internal definitions.
* libguile/pairs.c (scm_set_car_x, scm_set_cdr_x): Validate mutable
pairs.
* libguile/alist.c (scm_assq_set_x, scm_assv_set_x, scm_assoc_set_x):
* libguile/list.c (scm_reverse_x, scm_list_set_x, scm_list_cdr_set_x):
* libguile/srcprop.c (scm_make_srcprops):
* libguile/srfi-1.c (scm_srfi1_append_reverse_x)
(scm_srfi1_delete_duplicates_x):
* libguile/symbols.c (scm_symbol_fset_x, scm_symbol_pset_x):
* libguile/sort.c (scm_merge_list_x): Use scm_set_car_x / scm_set_cdr_x
instead of the macros, so as to check for mutable pairs.
(SCM_VALIDATE_MUTABLE_LIST): New internal helper macro.
(scm_sort_x, scm_stable_sort_x, scm_sort_list_x): Use
SCM_VALIDATE_MUTABLE_LIST.
* libguile/vm-engine.c (VM_VALIDATE_MUTABLE_PAIR): New definition.
(set-car!, set-cdr!): Use VM_VALIDATE_MUTABLE_PAIR. Fix error message
for set-cdr!.
2017-04-17 11:26:17 +02:00
|
|
|
|
scm_set_cdr_x (endret, lst);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
endret = lst;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
l = SCM_CDR (l);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* terminate, in case last element was deleted */
|
Attempt to mutate residualized literal pair throws exception
* libguile/validate.h (SCM_VALIDATE_MUTABLE_PAIR):
* libguile/pairs.h (scm_is_mutable_pair): New internal definitions.
* libguile/pairs.c (scm_set_car_x, scm_set_cdr_x): Validate mutable
pairs.
* libguile/alist.c (scm_assq_set_x, scm_assv_set_x, scm_assoc_set_x):
* libguile/list.c (scm_reverse_x, scm_list_set_x, scm_list_cdr_set_x):
* libguile/srcprop.c (scm_make_srcprops):
* libguile/srfi-1.c (scm_srfi1_append_reverse_x)
(scm_srfi1_delete_duplicates_x):
* libguile/symbols.c (scm_symbol_fset_x, scm_symbol_pset_x):
* libguile/sort.c (scm_merge_list_x): Use scm_set_car_x / scm_set_cdr_x
instead of the macros, so as to check for mutable pairs.
(SCM_VALIDATE_MUTABLE_LIST): New internal helper macro.
(scm_sort_x, scm_stable_sort_x, scm_sort_list_x): Use
SCM_VALIDATE_MUTABLE_LIST.
* libguile/vm-engine.c (VM_VALIDATE_MUTABLE_PAIR): New definition.
(set-car!, set-cdr!): Use VM_VALIDATE_MUTABLE_PAIR. Fix error message
for set-cdr!.
2017-04-17 11:26:17 +02:00
|
|
|
|
scm_set_cdr_x (endret, SCM_EOL);
|
2003-07-08 00:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* demand that lst was a proper list */
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-13 22:49:28 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM lst),
|
|
|
|
|
|
"Return the first element of @var{lst} which satisfies the\n"
|
|
|
|
|
|
"predicate @var{pred}, or return @code{#f} if no such element is\n"
|
|
|
|
|
|
"found.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_find
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, pred);
|
2005-03-13 22:49:28 +00:00
|
|
|
|
|
|
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM elem = SCM_CAR (lst);
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_1 (pred, elem)))
|
2005-03-13 22:49:28 +00:00
|
|
|
|
return elem;
|
|
|
|
|
|
}
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
return SCM_BOOL_F;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM lst),
|
|
|
|
|
|
"Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
|
|
|
|
|
|
"predicate @var{pred}, or return @code{#f} if no such element is\n"
|
|
|
|
|
|
"found.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_find_tail
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, pred);
|
2005-03-13 22:49:28 +00:00
|
|
|
|
|
|
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
|
2005-03-13 22:49:28 +00:00
|
|
|
|
return lst;
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
return SCM_BOOL_F;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2003-07-28 23:54:39 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
|
|
|
|
|
|
(SCM lst),
|
|
|
|
|
|
"Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
|
|
|
|
|
|
"circular.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_length_plus
|
|
|
|
|
|
{
|
2014-04-18 15:04:12 -04:00
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
SCM tortoise = lst;
|
|
|
|
|
|
SCM hare = lst;
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!scm_is_pair (hare))
|
2014-06-04 20:42:21 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_NULL_OR_NIL_P (hare))
|
|
|
|
|
|
return scm_from_size_t (i);
|
|
|
|
|
|
else
|
|
|
|
|
|
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
|
|
|
|
|
|
"proper or circular list");
|
|
|
|
|
|
}
|
2014-04-18 15:04:12 -04:00
|
|
|
|
hare = SCM_CDR (hare);
|
|
|
|
|
|
i++;
|
|
|
|
|
|
if (!scm_is_pair (hare))
|
2014-06-04 20:42:21 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_NULL_OR_NIL_P (hare))
|
|
|
|
|
|
return scm_from_size_t (i);
|
|
|
|
|
|
else
|
|
|
|
|
|
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
|
|
|
|
|
|
"proper or circular list");
|
|
|
|
|
|
}
|
2014-04-18 15:04:12 -04:00
|
|
|
|
hare = SCM_CDR (hare);
|
|
|
|
|
|
i++;
|
|
|
|
|
|
/* For every two steps the hare takes, the tortoise takes one. */
|
2014-06-04 20:42:21 -04:00
|
|
|
|
tortoise = SCM_CDR (tortoise);
|
2014-04-18 15:04:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
while (!scm_is_eq (hare, tortoise));
|
|
|
|
|
|
|
|
|
|
|
|
/* If the tortoise ever catches the hare, then the list must contain
|
|
|
|
|
|
a cycle. */
|
|
|
|
|
|
return SCM_BOOL_F;
|
2003-07-28 23:54:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-22 22:36:18 +00:00
|
|
|
|
/* This routine differs from the core list-copy in allowing improper lists.
|
|
|
|
|
|
Maybe the core could allow them similarly. */
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
|
|
|
|
|
|
(SCM lst),
|
|
|
|
|
|
"Return a copy of the given list @var{lst}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} can be a proper or improper list. And if @var{lst}\n"
|
|
|
|
|
|
"is not a pair then it's treated as the final tail of an\n"
|
|
|
|
|
|
"improper list and simply returned.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_list_copy
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM newlst;
|
|
|
|
|
|
SCM * fill_here;
|
|
|
|
|
|
SCM from_here;
|
|
|
|
|
|
|
|
|
|
|
|
newlst = lst;
|
|
|
|
|
|
fill_here = &newlst;
|
|
|
|
|
|
from_here = lst;
|
|
|
|
|
|
|
2005-03-02 20:50:48 +00:00
|
|
|
|
while (scm_is_pair (from_here))
|
2003-08-22 22:36:18 +00:00
|
|
|
|
{
|
|
|
|
|
|
SCM c;
|
|
|
|
|
|
c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
|
|
|
|
|
|
*fill_here = c;
|
|
|
|
|
|
fill_here = SCM_CDRLOC (c);
|
|
|
|
|
|
from_here = SCM_CDR (from_here);
|
|
|
|
|
|
}
|
|
|
|
|
|
return newlst;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2005-11-23 23:56:08 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
|
|
|
|
|
|
(SCM equal, SCM lst, SCM rest),
|
|
|
|
|
|
"Return @var{lst} with any elements in the lists in @var{rest}\n"
|
|
|
|
|
|
"removed (ie.@: subtracted). For only one @var{lst} argument,\n"
|
|
|
|
|
|
"just that list is returned.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The given @var{equal} procedure is used for comparing elements,\n"
|
|
|
|
|
|
"called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
|
|
|
|
|
|
"is from @var{lst} and the second from one of the subsequent\n"
|
|
|
|
|
|
"lists. But exactly which calls are made and in what order is\n"
|
|
|
|
|
|
"unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@example\n"
|
|
|
|
|
|
"(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
|
|
|
|
|
|
"(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
|
|
|
|
|
|
"(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
|
|
|
|
|
|
"@end example\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@code{lset-difference!} may modify @var{lst} to form its\n"
|
|
|
|
|
|
"result.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_lset_difference_x
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM ret, *pos, elem, r, b;
|
|
|
|
|
|
int argnum;
|
|
|
|
|
|
|
2009-08-21 00:38:48 +02:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, equal);
|
2005-11-23 23:56:08 +00:00
|
|
|
|
SCM_VALIDATE_REST_ARGUMENT (rest);
|
|
|
|
|
|
|
|
|
|
|
|
ret = SCM_EOL;
|
|
|
|
|
|
pos = &ret;
|
|
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
|
|
|
|
|
{
|
|
|
|
|
|
elem = SCM_CAR (lst);
|
|
|
|
|
|
|
|
|
|
|
|
for (r = rest, argnum = SCM_ARG3;
|
|
|
|
|
|
scm_is_pair (r);
|
|
|
|
|
|
r = SCM_CDR (r), argnum++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_2 (equal, elem, SCM_CAR (b))))
|
2005-11-23 23:56:08 +00:00
|
|
|
|
goto next_elem; /* equal to elem, so drop that elem */
|
|
|
|
|
|
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* elem not equal to anything in later lists, so keep it */
|
|
|
|
|
|
*pos = lst;
|
|
|
|
|
|
pos = SCM_CDRLOC (lst);
|
|
|
|
|
|
|
|
|
|
|
|
next_elem:
|
|
|
|
|
|
;
|
|
|
|
|
|
}
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
*pos = SCM_EOL;
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-01 13:56:11 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
|
|
|
|
|
|
(SCM key, SCM alist, SCM pred),
|
2012-01-11 23:33:01 -05:00
|
|
|
|
"Behaves like @code{assq} but uses third argument @var{pred}\n"
|
|
|
|
|
|
"for key comparison. If @var{pred} is not supplied,\n"
|
2002-12-01 13:56:11 +00:00
|
|
|
|
"@code{equal?} is used. (Extended from R5RS.)\n")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_assoc
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM ls = alist;
|
|
|
|
|
|
scm_t_trampoline_2 equal_p;
|
|
|
|
|
|
if (SCM_UNBNDP (pred))
|
|
|
|
|
|
equal_p = equal_trampoline;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG3, pred);
|
|
|
|
|
|
equal_p = scm_call_2;
|
2002-12-01 13:56:11 +00:00
|
|
|
|
}
|
2005-03-02 20:50:48 +00:00
|
|
|
|
for(; scm_is_pair (ls); ls = SCM_CDR (ls))
|
2002-12-01 13:56:11 +00:00
|
|
|
|
{
|
|
|
|
|
|
SCM tmp = SCM_CAR (ls);
|
2005-03-02 20:50:48 +00:00
|
|
|
|
SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
|
2002-12-01 13:56:11 +00:00
|
|
|
|
"association list");
|
2006-06-17 22:47:50 +00:00
|
|
|
|
if (scm_is_true (equal_p (pred, key, SCM_CAR (tmp))))
|
2002-12-01 13:56:11 +00:00
|
|
|
|
return tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
|
|
|
|
|
|
"association list");
|
|
|
|
|
|
return SCM_BOOL_F;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2003-07-13 23:05:31 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM list),
|
|
|
|
|
|
"Partition the elements of @var{list} with predicate @var{pred}.\n"
|
fix typos in the manual bits generated from source comments.
* libguile/bitvectors.c, libguile/chars.c,
libguile/deprecated.c, libguile/numbers.c, libguile/random.c,
libguile/read.c, libguile/root.c, libguile/srfi-1.c,
libguile/srfi-13.c, libguile/srfi-14.c, libguile/uniform.c:
Fix typos, add missing newlines.
2011-02-07 00:29:51 +01:00
|
|
|
|
"Return two values: the list of elements satisfying @var{pred} and\n"
|
2003-07-13 23:05:31 +00:00
|
|
|
|
"the list of elements @emph{not} satisfying @var{pred}. The order\n"
|
|
|
|
|
|
"of the output lists follows the order of @var{list}. @var{list}\n"
|
|
|
|
|
|
"is not mutated. One of the output lists may share memory with @var{list}.\n")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_partition
|
|
|
|
|
|
{
|
|
|
|
|
|
/* In this implementation, the output lists don't share memory with
|
|
|
|
|
|
list, because it's probably not worth the effort. */
|
2008-04-28 18:03:27 +02:00
|
|
|
|
SCM orig_list = list;
|
2003-07-13 23:05:31 +00:00
|
|
|
|
SCM kept = scm_cons(SCM_EOL, SCM_EOL);
|
|
|
|
|
|
SCM kept_tail = kept;
|
|
|
|
|
|
SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
|
|
|
|
|
|
SCM dropped_tail = dropped;
|
|
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, pred);
|
2003-07-13 23:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
|
2008-04-28 18:03:27 +02:00
|
|
|
|
SCM elt, new_tail;
|
|
|
|
|
|
|
|
|
|
|
|
/* Make sure LIST is not a dotted list. */
|
|
|
|
|
|
SCM_ASSERT (scm_is_pair (list), orig_list, SCM_ARG2, FUNC_NAME);
|
|
|
|
|
|
|
|
|
|
|
|
elt = SCM_CAR (list);
|
|
|
|
|
|
new_tail = scm_cons (SCM_CAR (list), SCM_EOL);
|
|
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_1 (pred, elt))) {
|
2003-07-13 23:05:31 +00:00
|
|
|
|
SCM_SETCDR(kept_tail, new_tail);
|
|
|
|
|
|
kept_tail = new_tail;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
SCM_SETCDR(dropped_tail, new_tail);
|
|
|
|
|
|
dropped_tail = new_tail;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/* re-use the initial conses for the values list */
|
|
|
|
|
|
SCM_SETCAR(kept, SCM_CDR(kept));
|
|
|
|
|
|
SCM_SETCDR(kept, dropped);
|
|
|
|
|
|
SCM_SETCAR(dropped, SCM_CDR(dropped));
|
|
|
|
|
|
SCM_SETCDR(dropped, SCM_EOL);
|
|
|
|
|
|
return scm_values(kept);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2005-03-15 21:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM lst),
|
|
|
|
|
|
"Split @var{lst} into those elements which do and don't satisfy\n"
|
|
|
|
|
|
"the predicate @var{pred}.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The return is two values (@pxref{Multiple Values}), the first\n"
|
|
|
|
|
|
"being a list of all elements from @var{lst} which satisfy\n"
|
|
|
|
|
|
"@var{pred}, the second a list of those which do not.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The elements in the result lists are in the same order as in\n"
|
|
|
|
|
|
"@var{lst} but the order in which the calls @code{(@var{pred}\n"
|
|
|
|
|
|
"elem)} are made on the list elements is unspecified.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"@var{lst} may be modified to construct the return lists.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_partition_x
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM tlst, flst, *tp, *fp;
|
|
|
|
|
|
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
|
2005-03-15 21:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
/* tlst and flst are the lists of true and false elements. tp and fp are
|
|
|
|
|
|
where to store to append to them, initially &tlst and &flst, then
|
|
|
|
|
|
SCM_CDRLOC of the last pair in the respective lists. */
|
|
|
|
|
|
|
|
|
|
|
|
tlst = SCM_EOL;
|
|
|
|
|
|
flst = SCM_EOL;
|
|
|
|
|
|
tp = &tlst;
|
|
|
|
|
|
fp = &flst;
|
|
|
|
|
|
|
|
|
|
|
|
for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
|
2005-03-15 21:15:24 +00:00
|
|
|
|
{
|
|
|
|
|
|
*tp = lst;
|
|
|
|
|
|
tp = SCM_CDRLOC (lst);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
*fp = lst;
|
|
|
|
|
|
fp = SCM_CDRLOC (lst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
|
|
|
|
|
|
|
|
|
|
|
|
/* terminate whichever didn't get the last element(s) */
|
|
|
|
|
|
*tp = SCM_EOL;
|
|
|
|
|
|
*fp = SCM_EOL;
|
|
|
|
|
|
|
|
|
|
|
|
return scm_values (scm_list_2 (tlst, flst));
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2005-01-28 21:53:47 +00:00
|
|
|
|
SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM list),
|
2012-01-11 23:33:01 -05:00
|
|
|
|
"Return a list containing all elements from @var{list} which do\n"
|
2005-01-28 21:53:47 +00:00
|
|
|
|
"not satisfy the predicate @var{pred}. The elements in the\n"
|
2012-01-11 23:33:01 -05:00
|
|
|
|
"result list have the same order as in @var{list}. The order in\n"
|
2005-01-28 21:53:47 +00:00
|
|
|
|
"which @var{pred} is applied to the list elements is not\n"
|
|
|
|
|
|
"specified.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_remove
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM walk;
|
|
|
|
|
|
SCM *prev;
|
|
|
|
|
|
SCM res = SCM_EOL;
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, pred);
|
2005-01-28 21:53:47 +00:00
|
|
|
|
SCM_VALIDATE_LIST (2, list);
|
|
|
|
|
|
|
|
|
|
|
|
for (prev = &res, walk = list;
|
|
|
|
|
|
scm_is_pair (walk);
|
|
|
|
|
|
walk = SCM_CDR (walk))
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
|
2005-01-28 21:53:47 +00:00
|
|
|
|
{
|
|
|
|
|
|
*prev = scm_cons (SCM_CAR (walk), SCM_EOL);
|
|
|
|
|
|
prev = SCM_CDRLOC (*prev);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2005-03-15 21:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
|
|
|
|
|
|
(SCM pred, SCM list),
|
|
|
|
|
|
"Return a list containing all elements from @var{list} which do\n"
|
|
|
|
|
|
"not satisfy the predicate @var{pred}. The elements in the\n"
|
|
|
|
|
|
"result list have the same order as in @var{list}. The order in\n"
|
|
|
|
|
|
"which @var{pred} is applied to the list elements is not\n"
|
|
|
|
|
|
"specified. @var{list} may be modified to build the return\n"
|
|
|
|
|
|
"list.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_srfi1_remove_x
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM walk;
|
|
|
|
|
|
SCM *prev;
|
2009-11-27 20:36:09 +01:00
|
|
|
|
SCM_VALIDATE_PROC (SCM_ARG1, pred);
|
2005-03-15 21:15:24 +00:00
|
|
|
|
SCM_VALIDATE_LIST (2, list);
|
|
|
|
|
|
|
|
|
|
|
|
for (prev = &list, walk = list;
|
|
|
|
|
|
scm_is_pair (walk);
|
|
|
|
|
|
walk = SCM_CDR (walk))
|
|
|
|
|
|
{
|
2009-11-27 20:36:09 +01:00
|
|
|
|
if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
|
2005-03-15 21:15:24 +00:00
|
|
|
|
prev = SCM_CDRLOC (walk);
|
|
|
|
|
|
else
|
|
|
|
|
|
*prev = SCM_CDR (walk);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
Start rewriting SRFI-1 in Scheme.
This partially reverts commit e556f8c3c6b74ee6596e8dcbe829109d7745da2c
(Fri May 6 2005).
* module/srfi/srfi-1.scm (xcons, list-tabulate, not-pair?, car+cdr,
last, fold, list-index): New procedures.
* srfi/srfi-1.c (srfi1_module): New variable.
(CACHE_VAR): New macro.
(scm_srfi1_car_plus_cdr, scm_srfi1_fold, scm_srfi1_last,
scm_srfi1_list_index, scm_srfi1_list_tabulate, scm_srfi1_not_pair_p,
scm_srfi1_xcons): Rewrite as proxies of the corresponding Scheme
procedure.
* test-suite/tests/srfi-1.test ("list-tabulate")["-1"]: Change exception
type to `exception:wrong-type-arg'.
* benchmark-suite/benchmarks/srfi-1.bm: New file.
* benchmark-suite/Makefile.am (SCM_BENCHMARKS): Add
`benchmarks/srfi-1.bm'.
* test-suite/standalone/Makefile.am (test_srfi_1_SOURCES,
test_srfi_1_CFLAGS, test_srfi_1_LDADD): New variables.
(check_PROGRAMS): Add `test-srfi-1'.
(TESTS): Ditto.
* test-suite/standalone/test-srfi-1.c: New file.
2010-07-13 00:07:12 +02:00
|
|
|
|
|
2010-09-12 23:18:54 +02:00
|
|
|
|
void
|
|
|
|
|
|
scm_register_srfi_1 (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
|
|
|
|
|
|
"scm_init_srfi_1",
|
|
|
|
|
|
(scm_t_extension_init_func)scm_init_srfi_1, NULL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-12-01 13:10:51 +00:00
|
|
|
|
void
|
|
|
|
|
|
scm_init_srfi_1 (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
#ifndef SCM_MAGIC_SNARFER
|
2010-09-12 23:18:54 +02:00
|
|
|
|
#include "libguile/srfi-1.x"
|
2002-12-01 13:10:51 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* End of srfi-1.c. */
|