From 84f85be7503e13c6427280017c1afa49d35bf7dc Mon Sep 17 00:00:00 2001 From: mainzelm Date: Thu, 26 Feb 2004 16:20:53 +0000 Subject: [PATCH] Merge from S48 repository Original commit message from Mike Sperber: Subject: Add implementation of FOR-EACH to SRFI-1 This was missing from Olin's reference implementation. Note that currently, the native versions of MAP and FOR-EACH already implement the extended semantics specified by SRFI 1. However, we might want to enforce the R5RS restrictions to MAP and FOR-EACH at some time in the future, so it still makes sense to have separate implementations in SRFI 1. --- scheme/srfi/srfi-1.scm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scheme/srfi/srfi-1.scm b/scheme/srfi/srfi-1.scm index 10d2ee5..1fac7a1 100644 --- a/scheme/srfi/srfi-1.scm +++ b/scheme/srfi/srfi-1.scm @@ -1027,6 +1027,26 @@ ;;; We extend MAP to handle arguments of unequal length. (define map map-in-order) +;;; Apply F across lists, guaranteeing to go left-to-right. +;;; NOTE: Some implementations of R5RS MAP are compliant with this spec; +;;; in which case this procedure may simply be defined as a synonym for FOR-EACH. + +(define (for-each f lis1 . lists) + (check-arg procedure? f for-each) + (if (pair? lists) + (let recur ((lists (cons lis1 lists))) + (receive (cars cdrs) (%cars+cdrs lists) + (if (pair? cars) + (begin + (apply f cars) ; Do head first, + (recur cdrs))))) ; then tail. + + ;; Fast path. + (let recur ((lis lis1)) + (if (not (null-list? lis)) + (begin + (f (car lis)) ; Do head first, + (recur (cdr lis))))))) ; then tail. ;;; filter, remove, partition ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;