Looking for clarification on 'map' in Racket
Here is one way to think about map
:
(map f (list 1 2 3)) computes (list (f 1) (f 2) (f 3)).
and
(map f (list 1 2 3) (list 11 22 33)) computes (list (f 1 11) (f 2 22) (f 3 33)).
So your example with +
becomes:
(map + (list 1 2 3) (list 11 22 33)) computes (list (+ 1 11) (+ 2 22) (+ 3 33))
which is (list 12 24 36)
.
In the beginning it with be clearer to write
(define f (lambda (x y) (+ x y)))
(map f (list 1 2 3) (list 11 22 33)))
but when you can get used to map
and lambda
, the shorthand
(map (lambda (x y) (+ x y)) (list 1 2 3) (list 11 22 33)))
is useful.
this assigns
'(1 2 3 4)
to variablenumber
,thenmap
performs(+ 1 '(1 2 3 4))
.
No, that's not what it does. map
is a looping function, it calls the function separately for each element in the list, and returns a list of the results.
So first it binds number
to 1
and performs (+ 1 number)
, which is (+ 1 1)
. Then it binds number
to 2
and performs (+ 1 number)
, which is (+ 1 2)
. And so on. All the results are collected into a list, so it returns (2 3 4 5)
.
Getting to your matrix operation, the matrix is represented as a list of lists, so we need nested loops, which are done using nested calls to map
.
(map (lambda (x y) (map + x y)) matrix_a matrix_b)
The outer map
works as follows: First it binds x
and y
to the first elements of matrix_a
and matrix_b
respectively, and performs (map + x y)
. Then it binds x
and y
to the second elements of matrix_a
and matrix_b
, and performs (map + x y)
. And so on for each element of the two lists. Finally it returns a list of all these results.
The inner (map + x y)
adds the corresponding elements of the two lists, returning a list of the sums. E.g. (map + '(1 2 3) '(4 5 6))
returns (5 7 9)
.
So all together this creates a list of lists, where each element is the sum of the corresponding elements of matrix_a
and matrix_b
.
Finally,
what does
(lambda x x)
exactly do?
It binds x
to the list of all the arguments, and returns that list. So ((lambda x x) 1 2 3 4)
returns the list (1 2 3 4)
. It's basically the inverse of apply
, which spreads a list into multiple arguments to a function.
(apply (lambda x x) some-list)
returns a copy of some-list
.