7

I defined a simple function:

disdn =
#(define-music-function
     (d)
     (ly:duration?)
   #{
    <dis ais dis g>#d
   #})

I am calling it as follows:

\disdn #8

I am getting an error:

wrong type for argument 1.  Expecting duration, found 8

What is the correct syntax for passing a duration? Please provide documentation reference as well.

1 Answer 1

8

You need to call your function either like this

\disdn 8

or like this

\disdn #(ly:make-duration 3)

When you write #8 you tell Lilypond to use the guile object 8, which is a number. But if you write 8 the parser will try to properly interpret this and will interpret this as duration.

For your code to work though you’d need to write

disdn =
#(define-music-function
     (d)
     (ly:duration?)
   #{
    <dis ais dis g>$d
   #})

Check the documentation for the difference between # and $: https://lilypond.org/doc/v2.24/Documentation/extending/lilypond-scheme-syntax

Basically # will mean the expression is lexed and then evaluated later by the parser. Meanwhile $ will mean that the expression is evaluated while it is being lexed, and the parser will parse the already evaluated expression. So writing

<...>#d

the parser will see <...> followed by a scheme expression and thus first evaluates <...> with the previous duration and then evaluates the scheme expression. But if you do

<dis ais dis g>$d

the parser will see <...> followed by a duration, and will thus evaluate this with the given duration.

4
  • I do not know why, but this site seems to turn <something>$d into a TAB clef ...
    – Lazy
    Commented Jan 18, 2023 at 12:48
  • Also, is the "3" supposed to be an "8" in your second code block? Commented Jan 18, 2023 at 13:22
  • 2
    @ElementsinSpace No. ly:make-duration takes a duration-log, so for a whole note we use 0, for a half 1, for a quarter 2, for an eigth 3 &c.
    – Lazy
    Commented Jan 18, 2023 at 13:31
  • (cleaning up my previous comments) The site is trying render the Markdown code block as ABCjs (a similar bug sometimes happens with jTab). To prevent this, use the HTML preformatted text delimiters <pre> & </pre> around the code block, and replace angle brackets < & > with their HTML entities &lt; & &gt;. Commented Jan 19, 2023 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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