How to define a variadic function

后端 未结 2 688
甜味超标
甜味超标 2021-02-20 14:17

I\'m looking for something similar to Javascript\'s arguments array:

function parent(){
  child.apply(this.arguments);
}

I\'m awar

2条回答
  •  抹茶落季
    2021-02-20 15:02

    The correct syntax is:

    (define (parent . args-list)
        )
    

    Use it like this:

    (parent 1 2 3 4 5)
    

    Inside the procedure, all the arguments will be bound to a list named args-list. In the above snippet, args-list will have '(1 2 3 4 5) as its value. This is an example of how variadic functions work in Scheme.

    For the sake of completeness, the same mechanism can be used for anonymous functions, too (notice that args-list is not surrounded by parenthesis):

    ((lambda args-list ) 1 2 3 4 5)
    

提交回复
热议问题