lilypond-user
[Top][All Lists]
Advanced

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

Re: Scheme help request: How can else of an if-statement be made to do n


From: Aaron Hill
Subject: Re: Scheme help request: How can else of an if-statement be made to do nothing?
Date: Fri, 11 Dec 2020 11:33:56 -0800
User-agent: Roundcube Webmail/1.4.9

On 2020-12-11 10:43 am, Matthew Fong wrote:
Hello everyone,

Resurrecting an old thread. I've been trying my hand in Scheme programming,
via small examples on the project I'm working on.

I wanted to extend the variable list to this function Harm wrote, to take an extra boolean variable, which is pretty trivial. The boolean is meant to change the color of the text depending if it's true or false. But I am not clear on how to define the color using the boolean. The trivial way to do it is adding a conditional under the first if, and duplicate all the code
except the color. Is there a proper Scheme-ish way to do this?
(Admittingly, I have not caught on to Scheme just yet -- my brain tends to
work with Python)

print-if-defined =
#(define-void-function (sym text) (symbol? markup?)
  (if (defined? sym)
      (add-text #{ \markup \with-color #'(0.8 0.2 0.2) #text #})))

symA = "Something"

\print-if-defined #'symB "Text"
\print-if-defined #'symA "Text"

Unless I am missing something from your specific use case, you should only need to nest another (if ...) expression in place of the actual color:

%%%%
print-if-defined =
#(define-void-function (foo sym text) ((boolean? #f) symbol? markup?)
  (if (defined? sym)
      (add-text #{ \markup \with-color
        #(if foo '(0.8 0.2 0.2) '(0.2 0.8 0.2))
        #text #})))

symA = "Something"

\print-if-defined #'symB "Text"     % hidden
\print-if-defined #'symA "Text"     % shown, green
\print-if-defined ##t #'symA "Text" % shown, red
%%%%


-- Aaron Hill



reply via email to

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