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

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

Could we have FOR in typed scheme please? Pretty please?


On Apr 26, 2009, at 12:53 PM, Sam TH wrote:

> Here are two more ways to do it (untested):
>
> #lang scheme
>
> (for ([a #(1 2 3)] [b #(1 2 3)])
>    (display (+ a b)))
>
> (require srfi/43)
> (vector-for-each (lambda (i a b) (display (+ a b))) #(1 2 3) #(1 2 3))
>
>
> 2009/4/26 Matthias Felleisen <matthias at ccs.neu.edu>:
>>
>> 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
>>
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>
>
>
> -- 
> sam th
> samth at ccs.neu.edu



Posted on the users mailing list.