Scheme's special map
As mentioned in the previous article, the map function does the following:
map f(x) [1, 2, 3, 4, 5]
-> [f(1), f(2), f(3), f(4), f(5)]
The Scheme family of languages provide a more advanced map function which allows the following:
map f(x, y) [1, 2, 3, 4] [a, b, c, d]
-> [f(1, a), f(2, b), f(3, c), f(4, d)]
This version of map can take multiple lists, and a function with as many arguments as there are lists, and apply over every list at once to return a single list of answers. The only requirement is that all given lists must be the same length.
For more examples:
(define (zip lst1 lst2)
(map list lst1 lst2))
(define (add-index lst)
(map cons (range (length lst)) lst))
(define (square-list lst)
(map * lst lst))