lilypond-user
[Top][All Lists]

## Re: Having trouble understanding optional and variable amount of argumen

 From: Stefano Troncaro Subject: Re: Having trouble understanding optional and variable amount of arguments Date: Thu, 1 Mar 2018 14:31:44 -0300

I didn't know about \default or the dot/comma separated number/symbol lists! I can see those being useful in some circumstances. I was thinking about cases where an undefined amount of things different than symbols or numbers are required, and the closest I can imagine is chaining functions to create the illusion of a variable amount of arguments, like this:
\version "2.19.80"

#(define (end-list? obj)
(and (list? obj)
(let ((item (last obj)))
(and (symbol? item)
(equal? "end" (symbol->string item))))))

end = #(list 'end)

#(define (el->curated-el el)
(delete 'end el))

untilEnd =
#(define-void-function (proc el) (procedure? end-list?)
(let ((curated-el (el->curated-el el)))
(for-each
(lambda (elem)
(proc elem))
curated-el)))

selfAppending =
#(define-scheme-function (e-l) (end-list?)
(let ((self-input (list (cons 1 2) (cons 3 4))))
(append self-input e-l)))

selfAppendingInput =
#(define-scheme-function (input e-l) (scheme? end-list?)
(append (list input) e-l))

\relative c'' {
c d e f
\untilEnd #pretty-print
\selfAppending
\selfAppendingInput #'(some useful input?)
\selfAppendingInput #selfAppending
\selfAppending
\end
g a b c
}

This structure just happens to work for something I'm trying now but I can see it being too narrow in general.

@Urs, I not familiar with \with blocks, I'll take a look at the oll-core code and experiment a bit with it. Maybe I'll be able to help.

2018-03-01 4:55 GMT-03:00 David Kastrup :

> Thank you! I see that this is not an option then. Also, I now understand
> why I couldn't make the optional arguments work, since I always left them
> for last.
>
> Do you know if it is possible to have a flexible amount of optional
> arguments that appear before the last mandatory one? Say, for example
> (define-music-function (arg1 args music) (number? ??? ly:music?) where arg1
> and music are mandatory, and basically everything between arg1 and the next
> music _expression_ is compacted into a list and accessed as args in the body
> of the function. Not with that syntax necessarily, but something that
> allows for that kind of usage?

You know that a number or symbol list can be entered as a
comma-separated list?

--
David Kastrup