3

How do I make an argument in a scheme function optional?

This says

A Scheme type predicate for which argN must return #t. There is also a special form (predicate? default) for specifying optional arguments.

Unfortunately, this does not tell me what I need to write - at least not in a way I understand.


Here is my scheme function:

pageSong = 
#(define-scheme-function
   (title music stanzas)
   (string? ly:music? markup?)
      #{
        \bookpart { 
          \header {
            title = $title
          } 
          \score {
            $music
            \layout {}
          }
          $stanzas
        }
      #})

I want stanzas to be optional. How do I need to modify my code to achieve that? I tried replacing markup? by (predicate? default), but that didn't do the trick.

4

An optional argument at the end of the argument list is not all that optional: the specified default is only used when you explicitly write \default in place of the "optional" argument in the call of the scheme function.

Usually you want optional arguments be recognised as being omitted because the specified predicate (in your case, markup?) does not fit what comes next. So you'd want to use something like

pageSong = 
#(define-scheme-function
   (title stanzas music)
   (string? (markup? empty-markup) ly:music?)
      #{
        \bookpart { 
          \header {
            title = $title
          } 
          \score {
            $music
            \layout {}
          }
          $stanzas
        }
      #})

And then either write some markup for stanzas before the music or not. In the latter case, since the music will usually not be in a form LilyPond can interpret as markup, LilyPond will then substitute empty-markup for the stanzas.

It's possible that you want a markup-list? instead (a single markup can never be broken across pages); in that case you'd use '() for the default empty markup list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.