unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* filter-map tail recursion
@ 2004-11-30 23:18 Kevin Ryde
  2004-12-22 16:49 ` Marius Vollmer
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Ryde @ 2004-11-30 23:18 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: filter-map tail recursion
  2004-11-30 23:18 filter-map tail recursion Kevin Ryde
@ 2004-12-22 16:49 ` Marius Vollmer
  2004-12-22 21:33   ` Kevin Ryde
  0 siblings, 1 reply; 4+ messages in thread
From: Marius Vollmer @ 2004-12-22 16:49 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> I'm thinking of this for 1.6 too, if 1.8 is still a while away.

Yes, not being tail-recursive is a bug.  Please fix this in 1.6, too.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: filter-map tail recursion
  2004-12-22 16:49 ` Marius Vollmer
@ 2004-12-22 21:33   ` Kevin Ryde
  2004-12-23  4:14     ` Marius Vollmer
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Ryde @ 2004-12-22 21:33 UTC (permalink / raw)
  Cc: guile-devel

Marius Vollmer <marius.vollmer@uni-dortmund.de> writes:
>
> Yes, not being tail-recursive is a bug.  Please fix this in 1.6, too.

I did all the srfi-1 places I could spot, they're in 1.6.7.  I didn't
get to `append!', but that's just a C-code non-tail so it takes longer
to die.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: filter-map tail recursion
  2004-12-22 21:33   ` Kevin Ryde
@ 2004-12-23  4:14     ` Marius Vollmer
  0 siblings, 0 replies; 4+ messages in thread
From: Marius Vollmer @ 2004-12-23  4:14 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> Marius Vollmer <marius.vollmer@uni-dortmund.de> writes:
> >
> > Yes, not being tail-recursive is a bug.  Please fix this in 1.6, too.
> 
> I did all the srfi-1 places I could spot, they're in 1.6.7.

Excellent, thanks!


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-12-23  4:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-30 23:18 filter-map tail recursion Kevin Ryde
2004-12-22 16:49 ` Marius Vollmer
2004-12-22 21:33   ` Kevin Ryde
2004-12-23  4:14     ` Marius Vollmer

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).