guile-commits
[Top][All Lists]
Advanced

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

[Guile-commits] 02/10: srfi-1: Rewrite 'find-tail' in Scheme.


From: Ludovic Courtès
Subject: [Guile-commits] 02/10: srfi-1: Rewrite 'find-tail' in Scheme.
Date: Wed, 17 Jun 2020 18:32:10 -0400 (EDT)

civodul pushed a commit to branch master
in repository guile.

commit cd4c747fb8cd6c65f18471aad54427d87b884ebc
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Wed Jun 17 17:33:28 2020 +0200

    srfi-1: Rewrite 'find-tail' in Scheme.
    
    * libguile/srfi-1.c (scm_srfi1_find_tail): Remove.
    * libguile/srfi-1.h (scm_srfi1_find_tail): Likewise.
    * module/srfi/srfi-1.scm (find-tail): New procedure.
---
 libguile/srfi-1.c      | 18 ------------------
 libguile/srfi-1.h      |  1 -
 module/srfi/srfi-1.scm | 11 +++++++++++
 3 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/libguile/srfi-1.c b/libguile/srfi-1.c
index 39291a4..1651bcd 100644
--- a/libguile/srfi-1.c
+++ b/libguile/srfi-1.c
@@ -575,24 +575,6 @@ SCM_DEFINE (scm_srfi1_delete_duplicates_x, 
"delete-duplicates!", 1, 1, 0,
 }
 #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
-{
-  SCM_VALIDATE_PROC (SCM_ARG1, pred);
-
-  for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
-    if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
-      return lst;
-  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_length_plus, "length+", 1, 0, 0,
             (SCM lst),
            "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
diff --git a/libguile/srfi-1.h b/libguile/srfi-1.h
index fa21dc4..3faaaa4 100644
--- a/libguile/srfi-1.h
+++ b/libguile/srfi-1.h
@@ -33,7 +33,6 @@ SCM_INTERNAL SCM scm_srfi1_delete (SCM x, SCM lst, SCM pred);
 SCM_INTERNAL SCM scm_srfi1_delete_x (SCM x, SCM lst, SCM pred);
 SCM_INTERNAL SCM scm_srfi1_delete_duplicates (SCM lst, SCM pred);
 SCM_INTERNAL SCM scm_srfi1_delete_duplicates_x (SCM lst, SCM pred);
-SCM_INTERNAL SCM scm_srfi1_find_tail (SCM pred, SCM lst);
 SCM_INTERNAL SCM scm_srfi1_length_plus (SCM lst);
 SCM_INTERNAL SCM scm_srfi1_lset_difference_x (SCM equal, SCM lst, SCM rest);
 SCM_INTERNAL SCM scm_srfi1_list_copy (SCM lst);
diff --git a/module/srfi/srfi-1.scm b/module/srfi/srfi-1.scm
index e5b28e7..1fc7a0e 100644
--- a/module/srfi/srfi-1.scm
+++ b/module/srfi/srfi-1.scm
@@ -731,6 +731,17 @@ the list returned."
                head
                (loop (cdr lst)))))))
 
+(define (find-tail pred lst)
+  "Return the first pair of @var{lst} whose @sc{car} satisfies the
+predicate @var{pred}, or return @code{#f} if no such element is found."
+  (check-arg procedure? pred find)
+  (let loop ((lst lst))
+    (and (not (null? lst))
+         (let ((head (car lst)))
+           (if (pred head)
+               lst
+               (loop (cdr lst)))))))
+
 (define (take-while pred ls)
   "Return a new list which is the longest initial prefix of LS whose
 elements all satisfy the predicate PRED."



reply via email to

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