How can i use 'map' with a procedure on a list of lists

example :

(map + (list 1 2) (list 3 4) (list 5 6))

(9 12)

suppose now i have a list : (list (list 1 2) (list 3 4) (list 5 6))

and i want to do the same with the elements of the list, how can i do?

note here i have 3 elements in ((list 1 2) (list 3 4) (list 5 6)) but the solution must works for any number of elements.

i can not find a solution to this problem in Scheme ,and it is not the first time i have this problem.

this is really a problem in scheme, for this reason some,srfi give methods working both on elements and on container containing the elements (vector or list)
example:

(array-ref array k ...)
(array-ref array index)
Returns the contents of the element of array at index k .... The sequence k ... must be a valid index to array. In the second form, index must be either a vector or a 0-based 1-dimensional array containing k

in SRFI 25: Multi-dimensional Array Primitives

what i want to do is a sum of lists taking advantage of the n-arity of + operator......

presented differently, i have a list of elements (e1 e2 ... eN) in scheme and i want to do
(map + e1 e2 ... eN)

eN being a list of numbers ,each eN of the same length

if i remember ,for example Kawa scheme allow unsplicing @lst outside of a back-quote
but this non standart

Unless I'm misunderstanding what you're asking, it looks like apply is what you want:

> (define xss (list (list 1 2)
                    (list 3 4)
                    (list 5 6)))
> (apply map + xss)
'(9 12)
1 Like

yes ,thank you

i should have find that myself :cry:

but why apply accepts map + ?
anyway i should have found this solution:

(define xss2 (cons + xss))
(apply map xss2)
(9 12)

The Racket Guide's section on functions has a good write-up of what apply does.

In short, (apply f (list a b c)) is equivalent to (f a b c). It takes the list elements and gives them to the function as its arguments.

You can give apply additional arguments between the function and the list: (apply f x (list a b c)) is equivalent to (f x a b c). In the previous example, + becomes the first argument of map, and then the lists in xss become the remaining arguments.

1 Like