Re: Attribute String...

Subject: Re: Attribute String...
From: "Lassi A. Tuura" <Lassi.Tuura@xxxxxxx>
Date: Mon, 06 Oct 1997 10:34:21 +0200
I sent an implementation of `map', which was wrong, and James Clark
responds:
> That implementation is not correct.
> 
>   (map + '(1 2 3) '(4 5 6))
> 
> should return
> 
>   (5 7 9)
> 
> not
> 
>  (1 2 3 4 5 6)

Ouch.  Maybe I should spend more time reading the specs and less time
guessing what the interface should be.  Here is another candidate that
avoids computing the full `transpose' of the argument lists.  Any idea
whether it is better or worse in terms of memory usage if `map' is used
heavily?

(define (list-heads lists)
  (if (null? lists)
      '()
      (if (list? (car lists))
	  (cons (caar lists) (list-heads (cdr lists)))
	  (list (car lists)))))

(define (list-tails lists)
  (if (null? lists)
      '()
      (if (list? (car lists))
	  (if (null? (cdar lists))
	      (list-tails (cdr lists))
	      (cons (cdar lists) (list-tails (cdr lists))))
	  (cdr lists))))

(define (map proc #!rest lists)
  (if (null? lists)
      '()
      (let loop ((args (list-heads lists))
		 (rest (list-tails lists))
		 (result '()))
	(if (null? rest)
	    (reverse (cons (apply proc args) result))
	    (loop (list-heads rest)
		  (list-tails rest)
		  (cons (apply proc args) result))))))

//lat
-- 
Lassi.Tuura@xxxxxxx          There's no sunrise without a night

 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread