From 23cf161b16c638b63dc187bf4d6a6133af94cbcd Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Wed, 5 Nov 2008 11:50:51 +0000 Subject: [PATCH] Replaced map impl. with the better one. At least faster... --- tools/capone/lib/capone.init | 42 ++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tools/capone/lib/capone.init b/tools/capone/lib/capone.init index 9cb9737..df0d5eb 100644 --- a/tools/capone/lib/capone.init +++ b/tools/capone/lib/capone.init @@ -187,15 +187,39 @@ (append cars (list car1)) (append cdrs (list cdr1)))))) -(define (map proc . lists) - (if (null? lists) - (apply proc) - (if (null? (car lists)) - '() - (let* ((unz (apply unzip1-with-cdr lists)) - (cars (car unz)) - (cdrs (cdr unz))) - (cons (apply proc cars) (apply map (cons proc cdrs))))))) +;; +;; Original implementation that pretty sucks +;; +;(define (map proc . lists) +; (if (null? lists) +; (apply proc) +; (if (null? (car lists)) +; '() +; (let* ((unz (apply unzip1-with-cdr lists)) +; (cars (car unz)) +; (cdrs (cdr unz))) +; (cons (apply proc cars) (apply map (cons proc cdrs))))))) + +(define (map1 proc lst) + (if (null? lst) + lst + (let loop ((lst lst) + (nls '())) + (if (null? lst) + (reverse nls) + (loop (cdr lst) + (cons (proc (car lst)) nls)))))) + +(define (map proc lst . more) + (if (null? more) + (map1 proc lst) + (let map-more ((lst lst) + (more more)) + (if (null? lst) + lst + (cons (apply proc (car lst) (map car more)) + (map-more (cdr lst) + (map cdr more))))))) ;; ;; Original implementation that pretty sucks