lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trying to write a scheme function


From: Valentin Petzel
Subject: Re: Trying to write a scheme function
Date: Fri, 04 Nov 2022 09:02:25 +0100

Hello David,

There are two main problems with your code:

c, d, e,2 are *not* valid scheme expressions. If you wrap one of these 
expressions in #{ ... #} like this

#(display #{ c, #})

you’ll see that c, is parsed as *pitch*, while e,2 is parsed as music of type 
NoteEvent. These can also be directly constructed using scheme.

If you enter c, as music you get music of type NoteEvent, with the same 
duration as the previous note (this is done by the parser).

So really your way of matching if the user entered c, cannot really be done, 
you can only match if the entered pitch was c, or if the note has a specific 
duration.

The second big problem is the way how you check pitch equality. The (= ...) is 
a numeric comparison and should thus only be used for numbers.

For checking equality we have three basic functions: eq?, eqv? and equal?. eq? 
checks if we reference the same object, but fails for numbers and strings. 
eqv? does the same, but with numbers and strings it compares the values. 
equal? is like eqv? but also allows comparing collections like lists for 
(content) equality.

So in your case you want to check equality of pitch or duration by using 
equal?).

About using a music function: If your function is intended to always return 
music, then probably yes.

Cheers,
Valentin

Am Freitag, 4. November 2022, 01:18:44 CET schrieb dfro:
> Hello everyone,
> I would like to create a function that transforms one note to another
> using scheme's cond procedure. This is what I have working so far using
> an arg of type number?:
> 
> %start code
> \version "2.22.2"
> 
> tNote =
> #(define-scheme-function
>    (note)
>    (number?)
>    (_i "Transform one note to another using cond")
>    (cond ((= note 1) #{ { c } #})
>          ((= note 2) #{ { c' } #})
>          ((= note 3) #{ { c''2 } #})
>          (else #{ s #})
>          )
>    )
> 
> 
> { \tNote 1 \tNote 5 \tNote 2 \tNote 3 }
> %end code
> 
> _________
> 
> I want the type to be ly:music? and the cond procedure to evaluate a
> note rather than a number. The following code does not work (I get an
> 'Unbound variable" error), but I think it shows what I am trying to do:
> 
> %start code
> \version "2.22.2"
> 
> tNote =
> #(define-scheme-function
>    (note)
>    (ly:music?)
>    (_i "Transform one note to another using cond")
>    (cond ((= note c,) #{ { c } #})
>          ((= note d,) #{ { c' } #})
>          ((= note e,2) #{ { c''2 } #})
>          (else #{ s #})
>          )
>    )
> 
> 
> { \tNote c, \tNote f \tNote d, \tNote e,2 }
> %end code
> 
> _________
> 
> Should I be creating a music function rather than a scheme function?
> 
> Any thoughts on how to get this working and why it works? Thanks for any
> help and for the mailing list.
> 
> Peace,
> David

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]