From d52f0bde95daa57f56ca61e42d732aa052704f6e Mon Sep 17 00:00:00 2001 From: Kevin Ryde Date: Sun, 28 May 2006 00:09:17 +0000 Subject: [PATCH] (append-reverse, append-reverse!): Rewrite in C. --- srfi/srfi-1.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ srfi/srfi-1.h | 2 ++ srfi/srfi-1.scm | 9 -------- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/srfi/srfi-1.c b/srfi/srfi-1.c index c2d9b4e71..8e27ab4e6 100644 --- a/srfi/srfi-1.c +++ b/srfi/srfi-1.c @@ -124,6 +124,65 @@ SCM_DEFINE (scm_srfi1_alist_copy, "alist-copy", 1, 0, 0, #undef FUNC_NAME + +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; + + while (scm_is_pair (revhead)) + { + /* 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 + + SCM_DEFINE (scm_srfi1_break, "break", 2, 0, 0, (SCM pred, SCM lst), "Return two values, the longest initial prefix of @var{lst}\n" diff --git a/srfi/srfi-1.h b/srfi/srfi-1.h index 810f1046d..936586697 100644 --- a/srfi/srfi-1.h +++ b/srfi/srfi-1.h @@ -33,6 +33,8 @@ #endif SCM_SRFI1_API SCM scm_srfi1_alist_copy (SCM alist); +SCM_SRFI1_API SCM scm_srfi1_append_reverse (SCM revhead, SCM tail); +SCM_SRFI1_API SCM scm_srfi1_append_reverse_x (SCM revhead, SCM tail); SCM_SRFI1_API SCM scm_srfi1_break (SCM pred, SCM lst); SCM_SRFI1_API SCM scm_srfi1_break_x (SCM pred, SCM lst); SCM_SRFI1_API SCM scm_srfi1_car_plus_cdr (SCM pair); diff --git a/srfi/srfi-1.scm b/srfi/srfi-1.scm index 30e536f5d..a7876b711 100644 --- a/srfi/srfi-1.scm +++ b/srfi/srfi-1.scm @@ -324,15 +324,6 @@ ;;; Miscelleneous: length, append, concatenate, reverse, zip & count -(define (append-reverse rev-head tail) - (let lp ((l rev-head) (acc tail)) - (if (null? l) - acc - (lp (cdr l) (cons (car l) acc))))) - -(define (append-reverse! rev-head tail) - (append-reverse rev-head tail)) ; XXX:optimize - (define (zip clist1 . rest) (let lp ((l (cons clist1 rest)) (acc '())) (if (any null? l)