[plt-scheme] for-each in vector (macro)

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Apr 26 12:39:52 EDT 2009

Here are two solutions:

#lang scheme

;; macros
(define-syntax for-each-vector
   (syntax-rules ()
     ((for-each-vector proc vec ...)
      (let ((len (min (vector-length vec) ...)))
        (do ((index 0 (+ index 1)))
          ((= index len))
          (proc (vector-ref vec index) ...))))))

(for-each-vector (lambda (a b) (display (+ a b))) #( 1 2 3)  #( 1 2 3))

;; functional, preferred

;; for-each-vector2  (All (A C ...) ((C ... -> A) (Vectorof C) ... ->  
A))
(define (for-each-vector2 p . vec)
   (for ((i (in-range (apply min (map vector-length vec)))))
        (apply p (map (lambda (v) (vector-ref v i)) vec))))

(for-each-vector2 (lambda (a b) (display (+ a b))) #( 1 2 3)  #( 1 2 3))

-- Matthias




On Apr 26, 2009, at 10:50 AM, François Michaudon wrote:

> Hello,
> (define-syntax for-each-vector
>   (syntax-rules ()
>     ((for-each-vector proc vec) (let ((len (vector-length vec)))
>                                      (do ((index 0 (+ index 1)))
>                                        ((= index len))
>                                        (proc (vector-ref vec  
> index)))))))
> Now examples:
> (for-each-vector (lambda (a) (display a)) #( 1 2 3))
>
> >>> 123 ok
>
> (for-each-vector (lambda (a b) (display (+ a b))) #( 1 2 3)  #( 1 2  
> 3))
>
> >>> error syntax
>
> I try to put some ... in my define doesn't work.
> How to use multiple args in lambda ?
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.