Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hello Aaron, This makes sense, and the let statement is something I was missing (!) mattfong On Fri, Dec 11, 2020 at 12:54 PM Aaron Hill wrote: > On 2020-12-11 12:30 pm, Matthew Fong wrote: > > Hello Aaron, > > > >> .< Oh boy, that is *simple*. I went off the deep end on this, trying > >> to > > make another variable that would get assigned the color. That clearly > > is > > not the way Scheme works. The inline conditional is a thing of beauty. > > > > Looks like I need to spend more time studying Scheme syntax. > > Defining a variable would make sense if you needed the value in a few > places, since that would cut down on redundant expressions. But even if > you only needed the value once, it sometimes makes sense to use > variables to help keep other expressions simpler. The \markup below is > arguably easier to follow without the embedded Scheme expression: > > > print-if-defined = > #(define-void-function >(foo sym text) >((boolean? #f) symbol? markup?) >(let ((color (if foo '(0.8 0.2 0.2) '(0.2 0.8 0.2 > (if (defined? sym) > (add-text #{ \markup \with-color #color #text #} > > symA = "Something" > > \print-if-defined symB "Text" % hidden > \print-if-defined symA "Text" % shown, green > \print-if-defined ##t symA "Text" % shown, red > > > NOTE: LilyPond's parser is able to interpret "symA" as a symbol on its > own without needing to escape to Scheme syntax by writing #'symA. Just > something to keep in mind as I think it results in cleaner usage. > > > -- Aaron Hill >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
On 2020-12-11 12:30 pm, Matthew Fong wrote: Hello Aaron, .< Oh boy, that is *simple*. I went off the deep end on this, trying to make another variable that would get assigned the color. That clearly is not the way Scheme works. The inline conditional is a thing of beauty. Looks like I need to spend more time studying Scheme syntax. Defining a variable would make sense if you needed the value in a few places, since that would cut down on redundant expressions. But even if you only needed the value once, it sometimes makes sense to use variables to help keep other expressions simpler. The \markup below is arguably easier to follow without the embedded Scheme expression: print-if-defined = #(define-void-function (foo sym text) ((boolean? #f) symbol? markup?) (let ((color (if foo '(0.8 0.2 0.2) '(0.2 0.8 0.2 (if (defined? sym) (add-text #{ \markup \with-color #color #text #} symA = "Something" \print-if-defined symB "Text" % hidden \print-if-defined symA "Text" % shown, green \print-if-defined ##t symA "Text" % shown, red NOTE: LilyPond's parser is able to interpret "symA" as a symbol on its own without needing to escape to Scheme syntax by writing #'symA. Just something to keep in mind as I think it results in cleaner usage. -- Aaron Hill
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hello Aaron, >.< Oh boy, that is *simple*. I went off the deep end on this, trying to make another variable that would get assigned the color. That clearly is not the way Scheme works. The inline conditional is a thing of beauty. Looks like I need to spend more time studying Scheme syntax. Many thanks, mattfong On Fri, Dec 11, 2020 at 12:07 PM Aaron Hill wrote: > 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 > >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
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
Re: Scheme help request: How can else of an if-statement be made to do nothing?
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" Many thanks, mattfong
RE: Scheme help request: How can else of an if-statement be made to do nothing?
Ah, thank you much. John > -Original Message- > From: Thomas Morley [mailto:thomasmorle...@gmail.com] > Sent: Saturday, October 17, 2020 3:14 PM > To: John Schlomann > Cc: David Kastrup; Jean Abou Samra; Matthew Fong; lilypond-user > Subject: Re: Scheme help request: How can else of an if-statement be made > to do nothing? > > Am Sa., 17. Okt. 2020 um 22:09 Uhr schrieb John Schlomann > : > > > > Harm, > > > > You used a function or macro called add-text. I can't seem to find it in any > LilyPond or Guile manual. What is that? > > > > John > > Hi John, > > you'll find it in lily-library.scm > > Cheers, > Harm
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Thanks for the pointer, Harm. Matt On Sat, Oct 17, 2020 at 13:14 Thomas Morley wrote: > Am Sa., 17. Okt. 2020 um 22:09 Uhr schrieb John Schlomann > : > > > > Harm, > > > > You used a function or macro called add-text. I can't seem to find it in > any LilyPond or Guile manual. What is that? > > > > John > > Hi John, > > you'll find it in lily-library.scm > > Cheers, > Harm >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Am Sa., 17. Okt. 2020 um 22:09 Uhr schrieb John Schlomann : > > Harm, > > You used a function or macro called add-text. I can't seem to find it in any > LilyPond or Guile manual. What is that? > > John Hi John, you'll find it in lily-library.scm Cheers, Harm
RE: Scheme help request: How can else of an if-statement be made to do nothing?
Harm, You used a function or macro called add-text. I can't seem to find it in any LilyPond or Guile manual. What is that? John > -Original Message- > From: lilypond-user [mailto:lilypond-user- > bounces+jschlomann=wideopenwest@gnu.org] On Behalf Of Thomas > Morley > Sent: Saturday, October 17, 2020 2:24 PM > To: David Kastrup > Cc: Jean Abou Samra; Matthew Fong; lilypond-user > Subject: Re: Scheme help request: How can else of an if-statement be made > to do nothing? > > Am Sa., 17. Okt. 2020 um 20:33 Uhr schrieb David Kastrup : > > > > Thomas Morley writes: > > > > > Am Sa., 17. Okt. 2020 um 19:39 Uhr schrieb Jean Abou Samra > : > > >> > > >> The point of interpret-markup is to turn a markup into a stencil, so I'd > > >> use empty-stencil: > > >> > > >> \version "2.20.0" > > >> > > >> #(define-markup-command (print-if-defined layout props sym text) > > >> (symbol? markup?) > > >>(if (defined? sym) > > >>(interpret-markup layout props > > >> #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) > > >>empty-stencil)) > > >> > > >> symA = "Something" > > >> > > >> \markup { > > >>\print-if-defined #'symA "Text" > > >>\print-if-defined #'symB "More text" > > >> } > > >> > > > > > > An empty stencil will still be spaced (unless removed by other > > > markup-commands). > > > > empty-stencil will typically not trigger additional spacing. That's > > typically what distinguishes it from point-stencil . > > > > -- > > David Kastrup > > Well, I'd say it depends how that empty-stencil is consumed. See: > > \markup > \box > { \stencil #point-stencil \stencil #empty-stencil \stencil #point-stencil } > > Here I did something silly (of course there are warnings emitted) and > the output _is_ affected. > > Thus, the detailed use-case is important... > > Cheers, > Harm
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Am Sa., 17. Okt. 2020 um 20:33 Uhr schrieb David Kastrup : > > Thomas Morley writes: > > > Am Sa., 17. Okt. 2020 um 19:39 Uhr schrieb Jean Abou Samra > > : > >> > >> The point of interpret-markup is to turn a markup into a stencil, so I'd > >> use empty-stencil: > >> > >> \version "2.20.0" > >> > >> #(define-markup-command (print-if-defined layout props sym text) > >> (symbol? markup?) > >>(if (defined? sym) > >>(interpret-markup layout props > >> #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) > >>empty-stencil)) > >> > >> symA = "Something" > >> > >> \markup { > >>\print-if-defined #'symA "Text" > >>\print-if-defined #'symB "More text" > >> } > >> > > > > An empty stencil will still be spaced (unless removed by other > > markup-commands). > > empty-stencil will typically not trigger additional spacing. That's > typically what distinguishes it from point-stencil . > > -- > David Kastrup Well, I'd say it depends how that empty-stencil is consumed. See: \markup \box { \stencil #point-stencil \stencil #empty-stencil \stencil #point-stencil } Here I did something silly (of course there are warnings emitted) and the output _is_ affected. Thus, the detailed use-case is important... Cheers, Harm
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Thomas Morley writes: > Am Sa., 17. Okt. 2020 um 19:39 Uhr schrieb Jean Abou Samra > : >> >> The point of interpret-markup is to turn a markup into a stencil, so I'd >> use empty-stencil: >> >> \version "2.20.0" >> >> #(define-markup-command (print-if-defined layout props sym text) >> (symbol? markup?) >>(if (defined? sym) >>(interpret-markup layout props >> #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) >>empty-stencil)) >> >> symA = "Something" >> >> \markup { >>\print-if-defined #'symA "Text" >>\print-if-defined #'symB "More text" >> } >> > > An empty stencil will still be spaced (unless removed by other > markup-commands). empty-stencil will typically not trigger additional spacing. That's typically what distinguishes it from point-stencil . -- David Kastrup
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hello Jean and Thomas, Thank you -- both solutions are insightful and useful in future solutions. I did not know about empty stencils, and I read about a void-function earlier, but was not entirely sure how to utilize it. Many thanks, mattfong On Sat, Oct 17, 2020 at 10:49 AM Thomas Morley wrote: > Am Sa., 17. Okt. 2020 um 19:39 Uhr schrieb Jean Abou Samra < > j...@abou-samra.fr>: > > > > Hi, > > > > Le 17/10/2020 à 19:27, Matthew Fong a écrit : > > > > > Hello Richard, > > > > > > I just discovered your explanation is in fact the case, and also > > > concluded cond wouldn't work either. > > > > > > I might have to settle for an empty markup block for now. > > > > The point of interpret-markup is to turn a markup into a stencil, so I'd > > use empty-stencil: > > > > \version "2.20.0" > > > > #(define-markup-command (print-if-defined layout props sym text) > > (symbol? markup?) > >(if (defined? sym) > >(interpret-markup layout props > > #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) > >empty-stencil)) > > > > symA = "Something" > > > > \markup { > >\print-if-defined #'symA "Text" > >\print-if-defined #'symB "More text" > > } > > > > Best, > > Jean > > > > > > An empty stencil will still be spaced (unless removed by other > markup-commands). > Why not a void-function? > > 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" > > Cheers, > Harm >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Am Sa., 17. Okt. 2020 um 19:39 Uhr schrieb Jean Abou Samra : > > Hi, > > Le 17/10/2020 à 19:27, Matthew Fong a écrit : > > > Hello Richard, > > > > I just discovered your explanation is in fact the case, and also > > concluded cond wouldn't work either. > > > > I might have to settle for an empty markup block for now. > > The point of interpret-markup is to turn a markup into a stencil, so I'd > use empty-stencil: > > \version "2.20.0" > > #(define-markup-command (print-if-defined layout props sym text) > (symbol? markup?) >(if (defined? sym) >(interpret-markup layout props > #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) >empty-stencil)) > > symA = "Something" > > \markup { >\print-if-defined #'symA "Text" >\print-if-defined #'symB "More text" > } > > Best, > Jean > > An empty stencil will still be spaced (unless removed by other markup-commands). Why not a void-function? 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" Cheers, Harm
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hi, Le 17/10/2020 à 19:27, Matthew Fong a écrit : Hello Richard, I just discovered your explanation is in fact the case, and also concluded cond wouldn't work either. I might have to settle for an empty markup block for now. The point of interpret-markup is to turn a markup into a stencil, so I'd use empty-stencil: \version "2.20.0" #(define-markup-command (print-if-defined layout props sym text) (symbol? markup?) (if (defined? sym) (interpret-markup layout props #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) empty-stencil)) symA = "Something" \markup { \print-if-defined #'symA "Text" \print-if-defined #'symB "More text" } Best, Jean
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hello Richard, I just discovered your explanation is in fact the case, and also concluded cond wouldn't work either. I might have to settle for an empty markup block for now. Many thanks, mattfong On Sat, Oct 17, 2020 at 9:55 AM Richard Shann wrote: > On Sat, 2020-10-17 at 09:34 -0700, Matthew Fong wrote: > > Hello everyone, > > > > No less than 10 minutes after, I discovered I skipped the section on > > cond. That solves everything! > > I think you are missing something here: the if macro in Scheme does not > require an else expression > > (if a b) > > is fine, b will be evaluated if a is not #f > > your real problem is that the invocation of the procedure PrintDefined > will still evaluate to something even when you "do nothing" in the case > where the else expression would be evaluated. Your \markup will barf if > the invocation of \PrintDefined evaluates to something that \markup is > not expecting. > > Richard > > > > > > > > Many thanks, > > mattfong > > > > On Sat, Oct 17, 2020 at 9:27 AM Matthew Fong > > wrote: > > > Hello everyone, > > > > > > I've been digging into guile/scheme and LilyPond, and it doesn't > > > appear that else-statements can be empty. Any pointers would be > > > appreciated in advance. > > > > > > For the example below, I would prefer the else-statement here to do > > > nothing, rather than print an empty markup: \markup { "" }, as > > > doing so creates a small, but observable change in vertical > > > spacing. > > > > > > % Print markup if a symbol is defined > > > #(define-markup-command (PrintIfDefined layout props sym text) > > > (symbol? markup?) > > > (if (defined? sym) > > > ;; Do this if true > > > (interpret-markup layout props > > > #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) > > > > > > ;; How do I do nothing here? (empty statement preferred) > > > (interpret-markup layout props > > > #{ \markup "" #}) > > > ) > > > ) > > > > > > printInfo = 1 > > > \markup \PrintIfDefined #'printInfo "Print this if symbol printInfo > > > is defined" > > > > > > > > > Many thanks, > > > mattfong > > > > > > >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
On Sat, 2020-10-17 at 09:34 -0700, Matthew Fong wrote: > Hello everyone, > > No less than 10 minutes after, I discovered I skipped the section on > cond. That solves everything! I think you are missing something here: the if macro in Scheme does not require an else expression (if a b) is fine, b will be evaluated if a is not #f your real problem is that the invocation of the procedure PrintDefined will still evaluate to something even when you "do nothing" in the case where the else expression would be evaluated. Your \markup will barf if the invocation of \PrintDefined evaluates to something that \markup is not expecting. Richard > > > Many thanks, > mattfong > > On Sat, Oct 17, 2020 at 9:27 AM Matthew Fong > wrote: > > Hello everyone, > > > > I've been digging into guile/scheme and LilyPond, and it doesn't > > appear that else-statements can be empty. Any pointers would be > > appreciated in advance. > > > > For the example below, I would prefer the else-statement here to do > > nothing, rather than print an empty markup: \markup { "" }, as > > doing so creates a small, but observable change in vertical > > spacing. > > > > % Print markup if a symbol is defined > > #(define-markup-command (PrintIfDefined layout props sym text) > > (symbol? markup?) > > (if (defined? sym) > > ;; Do this if true > > (interpret-markup layout props > > #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) > > > > ;; How do I do nothing here? (empty statement preferred) > > (interpret-markup layout props > > #{ \markup "" #}) > > ) > > ) > > > > printInfo = 1 > > \markup \PrintIfDefined #'printInfo "Print this if symbol printInfo > > is defined" > > > > > > Many thanks, > > mattfong > > > >
Re: Scheme help request: How can else of an if-statement be made to do nothing?
Hello everyone, No less than 10 minutes after, I discovered I skipped the section on cond. That solves everything! Many thanks, mattfong On Sat, Oct 17, 2020 at 9:27 AM Matthew Fong wrote: > Hello everyone, > > I've been digging into guile/scheme and LilyPond, and it doesn't appear > that else-statements can be empty. Any pointers would be appreciated in > advance. > > For the example below, I would prefer the else-statement here to do > nothing, rather than print an empty markup: \markup { "" }, as doing so > creates a small, but observable change in vertical spacing. > > % Print markup if a symbol is defined > #(define-markup-command (PrintIfDefined layout props sym text) (symbol? > markup?) > (if (defined? sym) > ;; Do this if true > (interpret-markup layout props > #{ \markup \with-color #'(0.8 0.2 0.2) #text #}) >;; How do I do nothing here? (empty statement preferred) >(interpret-markup layout props >#{ \markup "" #}) > ) > ) > > printInfo = 1 > \markup \PrintIfDefined #'printInfo "Print this if symbol printInfo is > defined" > > > Many thanks, > mattfong > >