[plt-scheme] Newbie map question
I think you're looking for the function apply.
Robby
At Thu, 30 Dec 2004 13:22:41 +0100, Francisco Jesús Martínez Serrano wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hi, i'm learning scheme "the hard way", making a school project.
>
> I need to manipulate vectors and do some basic vector algebra.
>
> When faced with something as 'trivial' as the sum, I know I can sum
> vectors (lists) just as this:
>
> (map + '(1 2 3) '(4 5 6) '(4 5 6))
>
> The problem of this approach is that map + is not a procedure I can
> pass as an argument to a function (inner or outer product, for
> example), i need a self-contained map+. I can do this just by
> defining:
>
> (define map+
> (lambda (v1 v2)
> (map + v1 v2)))
>
> The problem of this approach is that it only works with 2 arguments. I
> know I can iterate through the list of vectors and accomplish it, but
> that seems too inefficient when a simple map + will do. Something like
> this:
>
> (define map+
> (lambda vecs
> (map + vecs)))
>
> or better:
>
> (define elementwise
> (lambda (f)
> (lambda vecs
> (map f vecs))))
>
> (define map+ (elementwise +))
>
> Which doesn't work because vecs is a list of vectors, and map expects
> each vector as an argument
>
> I know it's very simple, but can somebody give me a clue?
>
> Thanks.