Dear skilled cadencers,
I'd like to write a wrapper for the printf() function. The problem is that I do not know how to have a variable number of arguments. I tried with @rest:
procedure( myPrint(port str @rest args) fprintf(port str args) newline(port) )
however this won't work because args is a list and fprintf (or printf) complains about that. How can I achieve this task?
Take a look in the Skill Language Reference Index for "Comma-At" documentation. With this syntax you can get your function working:
procedure( myPrint(port str @rest args) apply('fprintf `(,port ,str ,@args) ) newline(port) ) port = outfile("/tmp/test.txt")myPrint(port "Hello %s %d" "derek" 1)myPrint(port "Hello %s %d" "joe" 2)myPrint(port "Hello %s %d" "zeke" 3)close(port)
The documentation states:In absence of commas and the comma-at (,@) construction, backquote functions in exactly the same way as single quote. However, if a comma appears inside a backquoted form, the expression that immediately follows the comma is evaluated, and the result of evaluation replaces the original form.The comma-at construction causes evaluation just as the comma does, but the results of evaluation must be a list, and the elements of the list, rather than the list itself, replace the original form.