Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a variadic function

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

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

I'm aware of the dot notation for variable argument lengths and also scheme's apply function.

This doesn't seem to work as the dot is taken to be the first argument:

(define (parent .) 
          (list .))

(parent 1 3 4 6 7)
Error: bad argument count - received 5 but expected 1: #<procedure (array arg-list)>

This works but isn't ideal. I'd like to call the function without the extra syntax to define the args list:

(define (parent args-list)
     (apply list args-list))


(parent 1 3 4 6 7)
Error: bad argument count - received 5 but expected 1: #<procedure (array args-list)>

(parent `(1 3 4 6 7))
(1 3 4 6 7)
like image 308
kevzettler Avatar asked Oct 17 '12 19:10

kevzettler


People also ask

How do you declare a variadic function in C++?

Variadic functions are functions (e.g. std::printf) which take a variable number of arguments. To declare a variadic function, an ellipsis appears after the list of parameters, e.g. int printf(const char* format...);, which may be preceded by an optional comma.

How do you call a variadic function?

You don't have to do anything special to call a variadic function. Just put the arguments (required arguments, followed by optional ones) inside parentheses, separated by commas, as usual. But you must declare the function with a prototype and know how the argument values are converted.

Which of the functions are variadic?

Variadic functions are functions (e.g. printf) which take a variable number of arguments. The declaration of a variadic function uses an ellipsis as the last parameter, e.g. int printf(const char* format, ...);. See variadic arguments for additional detail on the syntax and automatic argument conversions.

What is variadic function in Python?

Variadic parameters (Variable Length argument) are Python's solution to that problem. A Variadic Parameter accepts arbitrary arguments and collects them into a data structure without raising an error for unmatched parameters numbers.


2 Answers

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)
like image 176
Óscar López Avatar answered Oct 11 '22 14:10

Óscar López


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.

like image 33
leppie Avatar answered Oct 11 '22 13:10

leppie