What is the difference between map and apply in scheme?

前端 未结 3 2265
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 06:26

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

3条回答
  •  日久生厌
    2021-02-07 06:46

    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))

提交回复
热议问题