map 和 apply in scheme 有什么区别? [英] What is the difference between map and apply in scheme?

查看:68
本文介绍了map 和 apply in scheme 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 Scheme,但我很难理解 mapapply 之间的区别.

I am trying to learn Scheme and I am having a hard time understanding the difference between map and apply.

据我所知,map 将函数应用于列表的每个元素,而 apply 将某些东西应用于过程的参数.

As I understand, map applies the function to each element of the list, and apply applies something to the arguments of a procedure.

它们可以互换使用吗?

推荐答案

它们不一样!他们的名字实际上可以帮助记住哪个做什么.

They are not the same! Their names can actually help remember which does what.

map 将把一个过程和一个或多个列表作为参数.该过程将为列表的每个位置调用一次,使用该位置的元素列表作为参数:

map will take as argument one procedure and one or more lists. The procedure will be called once for each position of the lists, using as arguments the list of elements at that position:

(map - '(2 3 4))
; => (-2 -3 -4)

map 调用了 (- 2), (- 3), (- 4) 来构建列表.

map called (- 2), (- 3), (- 4) to build the list.

(map + '( 1  2  3)
       '(10 20 30))
; => (11 22 33)

map 调用(+ 1 10) (+ 2 20) (+ 3 30) 来构建清单.

map called (+ 1 10) (+ 2 20) (+ 3 30) to build the list.

(map * '(2 2 -1)
       '(0 3  4)
       '(5 4  2))
; => (0 24 -8)

map 调用 (* 2 0 5) (* 2 3 4) (* -1 4 2)来构建列表.

map called (* 2 0 5) (* 2 3 4) (* -1 4 2) to build the list.

map 之所以有这个名字,是因为它在一组值(在列表中)上实现了一个map"(函数):

map has that name because it implements a "map" (function) on a set of values (in the lists):

(map - '(2 3 4))
 arguments     mapping "-"     result
     2       === (- 2) ===>     -2
     3       === (- 3) ===>     -3
     4       === (- 4) ===>     -4

(map + '( 1  2  3)
       '(10 20 30))
 arguments      mapping "+"      result
    1 10     === (+ 1 10) ===>     11 
    2 20     === (+ 2 20) ===>     22
    3 30     === (+ 3 30) ===>     33

apply 将接受至少两个参数,第一个是过程,最后一个是列表.它将使用以下参数调用过程,包括列表中的参数:

apply will take at least two arguments, the first of them being a procedure and the last a list. It will call the procedure with the following arguments, including those inside the list:

(apply + '(2 3 4))
; => 9

这与(+ 2 3 4)

(apply display '("Hello, world!"))
; does not return a value, but prints "Hello, world!"

这与 (display "Hello, world!") 相同.

apply 当您将参数作为列表时很有用,

apply is useful when you have arguments as a list,

(define arguments '(10 50 100))
(apply + arguments)

如果你尝试重写最后一行而不使用 apply,你会意识到你需要循环遍历列表求和每个元素...

If you try to rewrite the last line without using apply, you'll realize that you need to loop over the list summing each element...

apply 也可以与不止这两个参数一起使用.第一个参数必须是可调用对象(过程或延续).最后一个必须是一个列表.其他(在第一个和最后一个之间)是任何类型的对象.所以打电话

apply may also be used with more than those two arguments. The first argument must be a callable object (a procedure or a continuation). The last one must be a list. The others (between the first and the last) are objects of any type. So calling

(apply PROC a b c ... y z '(one two ... twenty))

与调用相同

(PROC a b c ... y z  one two ... twenty)

这是一个具体的例子:

(apply + 1 -2 3 '(10 20))
; => 32

这与(+ 1 -2 3 10 20)

apply 有这个名字是因为它允许你将一个过程应用"到多个参数上.

apply has that name because it allows you to "apply" a procedure to several arguments.

这篇关于map 和 apply in scheme 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆