I am trying to learn Scheme and I am having a hard time understanding the difference between map
and apply
.
As I understand, map
a
No, apply
calls its first argument as a procedure, with all the rest as its arguments, with the last one -- list -- opened up, i.e. its contents "spliced in":
(apply f a b (list c d e)) == (f a b c d e)
E.g.:
(apply + 1 2 (list 3 4 5))
;Value:15
It is just one call; whereas map
is indeed calling its first argument for each member element of its second argument.
One combined use of map
and apply
is the famous transpose
trick:
(apply map list '((1 2 3) (10 20 30)))
;Value:((1 10) (2 20) (3 30))