如何在球拍中将列表作为参数列表传递? [英] How do I pass a list as a list of arguments in racket?
本文介绍了如何在球拍中将列表作为参数列表传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的声明:
((lambda (a b c) (+ a b c)) 1 2 3) ; Gives 6
我想也可以将其传递给列表:
And I would like to be able to also pass it a list as so:
((lambda (a b c) (+ a b c)) (list 1 2 3))
...除外,因为所有列表都以"a"形式传递,因此无法正常工作.有没有一种方法可以将列表分解为参数?
...except this doesn't work because the entire list is passed as 'a.' Is there is a way to explode the list into arguments?
我正在寻找的东西类似于Python中的*字符.对于那些不熟悉语法的人:
What I'm looking for is something similar to the * character in Python. For those of you unfamiliar with the syntax:
def sumthree(a, b, c):
print a + b + c
sumthree(1, 2, 3) # Prints 6
sumthree(*(1, 2, 3)) # Also prints 6
推荐答案
该操作称为apply
.
(apply + (list 1 2 3)) ; => 6
apply
扩展"最后一个参数;以前的所有参数都按原样传递.这些都是一样的:
apply
"expands" the last argument; any previous arguments are passed as is. So these are all the same:
(apply + 1 2 3 (list 4 5 6))
(apply + (list 1 2 3 4 5 6))
(+ 1 2 3 4 5 6)
这篇关于如何在球拍中将列表作为参数列表传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文