From: Kevin Ryde <user42@zip.com.au>
Subject: filter-map tail recursion
Date: Wed, 01 Dec 2004 10:18:46 +1100 [thread overview]
Message-ID: <87vfbndiy1.fsf@zip.com.au> (raw)
[-- Attachment #1: Type: text/plain, Size: 269 bytes --]
* srfi-1.scm (filter-map): Use tail recursion, to avoid stack overflow.
I'm thinking of this for 1.6 too, if 1.8 is still a while away. And
various other tail recursions I've put just in 1.8 too. Hitting
overflows on just a few thousand elements is no fun.
[-- Attachment #2: srfi-1.scm.filter-map.diff --]
[-- Type: text/plain, Size: 814 bytes --]
--- srfi-1.scm.~1.33.~ 2004-08-27 10:14:23.000000000 +1000
+++ srfi-1.scm 2004-09-28 17:59:16.000000000 +1000
@@ -567,20 +567,22 @@
(define (filter-map f clist1 . rest)
(if (null? rest)
- (let lp ((l clist1))
+ (let lp ((l clist1)
+ (rl '()))
(if (null? l)
- '()
+ (reverse! rl)
(let ((res (f (car l))))
(if res
- (cons res (lp (cdr l)))
- (lp (cdr l))))))
- (let lp ((l (cons clist1 rest)))
+ (lp (cdr l) (cons res rl))
+ (lp (cdr l) rl)))))
+ (let lp ((l (cons clist1 rest))
+ (rl '()))
(if (any1 null? l)
- '()
+ (reverse! rl)
(let ((res (apply f (map1 car l))))
(if res
- (cons res (lp (map1 cdr l)))
- (lp (map1 cdr l))))))))
+ (lp (map1 cdr l) (cons res rl))
+ (lp (map1 cdr l) rl)))))))
;;; Filtering & partitioning
[-- Attachment #3: srfi-1.test.filter-map.diff --]
[-- Type: text/plain, Size: 1261 bytes --]
--- srfi-1.test.~1.9.~ 2003-12-03 08:18:16.000000000 +1100
+++ srfi-1.test 2004-11-30 17:54:42.000000000 +1100
@@ -458,6 +458,50 @@
(drop '(a b . c) 2))))
;;
+;; filter-map
+;;
+
+(with-test-prefix "filter-map"
+
+ (with-test-prefix "one list"
+ (pass-if "(1)"
+ (equal? '(1) (filter-map noop '(1))))
+
+ (pass-if "(#f)"
+ (equal? '() (filter-map noop '(#f))))
+
+ (pass-if "(1 2)"
+ (equal? '(1 2) (filter-map noop '(1 2))))
+
+ (pass-if "(#f 2)"
+ (equal? '(2) (filter-map noop '(#f 2))))
+
+ (pass-if "(#f #f)"
+ (equal? '() (filter-map noop '(#f #f))))
+
+ (pass-if "(1 2 3)"
+ (equal? '(1 2 3) (filter-map noop '(1 2 3))))
+
+ (pass-if "(#f 2 3)"
+ (equal? '(2 3) (filter-map noop '(#f 2 3))))
+
+ (pass-if "(1 #f 3)"
+ (equal? '(1 3) (filter-map noop '(1 #f 3))))
+
+ (pass-if "(1 2 #f)"
+ (equal? '(1 2) (filter-map noop '(1 2 #f)))))
+
+ (with-test-prefix "two lists"
+ (pass-if "(1 2 3) (4 5 6)"
+ (equal? '(1 2 3) (filter-map noop '(1 2 3) '(4 5 6))))
+
+ (pass-if "(#f 2 3) (4 5)"
+ (equal? '(2) (filter-map noop '(#f 2 3) '(4 5))))
+
+ (pass-if "(4 #f) (1 2 3)"
+ (equal? '(4) (filter-map noop '(4 #f) '(1 2 3))))))
+
+;;
;; length+
;;
[-- Attachment #4: Type: text/plain, Size: 143 bytes --]
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel
next reply other threads:[~2004-11-30 23:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-30 23:18 Kevin Ryde [this message]
2004-12-22 16:49 ` filter-map tail recursion Marius Vollmer
2004-12-22 21:33 ` Kevin Ryde
2004-12-23 4:14 ` Marius Vollmer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87vfbndiy1.fsf@zip.com.au \
--to=user42@zip.com.au \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).