How do I pass a list as a list of arguments in racket?

前端 未结 2 1377
日久生厌
日久生厌 2020-12-17 15:34

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:

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 16:06

    That operation is called apply.

    (apply + (list 1 2 3))   ; => 6
    

    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)
    

提交回复
热议问题