如何将列表作为球拍中的参数列表传递?
我有这样的声明:
((lambda (a b c) (+ a b c)) 1 2 3) ; Gives 6
我希望也能够向它传递一个列表,如下所示:
((lambda (a b c) (+ a b c)) (list 1 2 3))
...除非这不起作用,因为整个列表都作为“a”传递。有没有办法将列表分解为参数?
我正在寻找类似于Python中的*字符的东西。对于那些不熟悉语法的人:
def sumthree(a, b, c):
print a + b + c
sumthree(1, 2, 3) # Prints 6
sumthree(*(1, 2, 3)) # Also prints 6
I have a statement like this:
((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))
...except this doesn't work because the entire list is passed as 'a.' Is there is a way to explode the list into arguments?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该操作称为
apply
。apply
“扩展”最后一个参数;任何先前的参数都按原样传递。所以这些都是一样的:That operation is called
apply
.apply
"expands" the last argument; any previous arguments are passed as is. So these are all the same:请注意以下定义
“.”使您能够将任意数量的参数传递给函数。您仍然可以拥有必需的参数
Pay attention to the following definition
'.' gives you ability to pass any number of arguments to a function. You can still have required arguments