6

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.

Elements in Space
  • 10,785
  • 2
  • 23
  • 67
kargirwar
  • 245
  • 6

1 Answers1

7

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.

Elements in Space
  • 10,785
  • 2
  • 23
  • 67
Lazy
  • 12,346
  • 1
  • 9
  • 31
  • I do not know why, but this site seems to turn $d into a TAB clef ... – Lazy Jan 18 '23 at 12:48
  • Also, is the "3" supposed to be an "8" in your second code block? – Elements in Space Jan 18 '23 at 13:22
  • 1
    @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 Jan 18 '23 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 `
    ` & `
    ` around the code block, and replace angle brackets `<` & `>` with their HTML entities `<` & `>`.
    – Elements in Space Jan 19 '23 at 02:17