How to define a variadic function

时光毁灭记忆、已成空白 提交于 2019-12-04 04:28:19

The correct syntax is:

(define (parent . args-list)
    <do something with 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 <do something with args-list>) 1 2 3 4 5)

You want:

(define (parent . args) 
   args) ; args is a list

Which is infact the 'default' implementation of list.

(define (list . x) x)

The error message when applying (define (parent . ) ...) seems wrong, also, the code should not have compiled in the first place as it is invalid syntax. Might imply a bug with the version of Chicken Scheme you are using.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!