lilypond-user
[Top][All Lists]
Advanced

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

Re: output some string


From: Gianmaria Lari
Subject: Re: output some string
Date: Sun, 17 Jun 2018 10:32:14 +0200



On Sat, 16 Jun 2018 at 21:49, Thomas Morley <address@hidden> wrote:


2018-06-16 16:56 GMT+02:00 Gianmaria Lari <address@hidden>:


On Fri, 15 Jun 2018 at 22:47, Thomas Morley <address@hidden> wrote:
2018-06-15 20:11 GMT+02:00 Aaron Hill <address@hidden>:
> On 2018-06-15 09:40, Gianmaria Lari wrote:
>>
>> I'm trying to write some code in scheme. At the moment I do it using
>> frescobaldi and I need some help for the output.
>> The following code
>>
>> \version "2.19.81"
>> #(define (f w) w)
>> $(f "Hello")
>>
>>
>> generates a pdf file containing "Hello".
>>
>> If my function f is called passing something that is not a string like in
>> the following code
>>
>> \version "2.19.81"
>> #(define (f w) w)
>> $(f '(a b c))
>>
>>
>> it doesn't work. How can I convert my data to a string?
>>
>> Thank you, g.
>> P.S. I have been able to write in the Lilypond log using  #(define (f w)
>> (write w))...
>
>
> You might want to look at the `format` function:
>
> https://www.gnu.org/software/guile/manual/html_node/Formatted-Output.html

`format` is surely pretty flexible.
The link points to the guile-2.2.3-manual, though regarding `format` I
don't think there are many differences to guile-1.8.
Other things differ substantially.

>
> This gives you quite a few options for formatting data and getting a string.
>
> `pretty-print` can also be useful if you want to dump an arbitrary
> _expression_ to the output.  You have to jump through a few hoops, though, to
> get its output as a string:
>
> %%%%
>   \version "2.19.81"
>   #(define (pretty-print-to-string arg)
>     (let ((o (open-output-string)) (s ""))
>       (pretty-print arg o)
>       (set! s (get-output-string o))
>       (close-output-port o)
>       s))
>   $(pretty-print-to-string '(a b #(c d e) f . g))
> %%%%

No need for this.
Afaik, default `object->string' does this already. It also takes an
optional argument for the printing-procedure.

With 2.19.81 in scheme-sandbox:

(object->string #{ cis1 #} display-scheme-music)
=>
"(make-music
  'NoteEvent
  'duration
  (ly:make-duration 0)
  'pitch
  (ly:make-pitch -1 0 1/2))

"

(object->string #{ cis1 #} display-lily-music)
=>
"cis1
"

(object->string (ly:make-pitch 0 0 0))
=>
"#<Pitch c' >"


If a list contains only strings it's also possible to do:

(string-concatenate '("foo" "bar" "buzz"))
=>
"foobarbuzz"


Cheers,
  Harm

Thank Harm!
My problem was to find a way to display, in the music view of frescobaldi, the result of simple function returning "simple" types: numbers, string, list. 
Your suggestion,  "object->string" looks the simplest way.

I don't see any advantage to print such things to the pdf.
But I can easily imagine disadvantages.  See below.

In my opinion. 
1 It's nice and useful to know how to print in Music view AND in the lilypond log.
2 The lilypond log displays compile log information, so it's not immediately clear the program output. In my opinion there are case (when you start to learn something) that you have advantages with maximum clearness. That's why I didn'tlike this:

\version "2.19.81"
$(write "This is a test\n")
$(write '(alfa beta gamma))

and this is the ouput

Starting lilypond-windows.exe 2.19.81 [Untitled (4)]...

Processing `C:/Users/GIANMA~1/AppData/Local/Temp/frescobaldi-1dd9dd_z/tmplnog9dtj/document.ly'

Parsing...

"This is a test

"(alfa beta gamma)


Success: compilation successfully completed

Completed successfully in 0.4".


I write here my test and my personal note, maybe other can take advantage of it. If you find some error please correct them. Thanks a lot, g.

Example code displaying simple type in the music view of frescobaldi

\version "2.19.81"
$(object->string 3)
$(object->string 3.48)
$(object->string #f)
$(object->string "Test")
$(object->string '(ciccio lippo))

Result:

image.png

Should be noted that, 

$(object->string 3)

is equivalent to

$(object->string 3 display) 

Nope. Default printing-procedure is `write`, see:

$(object->string "3")
$(object->string "3" display)
$(object->string "3" write)

Thank you!!!!


 


* * *

Some examples displaying a lilypond type, cis1. Please note the code is always the same except the optional argument.

\version "2.19.81"
$(object->string #{ cis1 #}) %no optional argument
image.png
(the output in the previous example is cut on the right!!)

What did you expect?

I expected lilypond would cut it:) But I write a note about it.
 
Console would usually break the lines of a long string. LilyPond only, if you tell her:

$(make-wordwrap-string-markup (object->string #{ cis1 #}))
[...]

Nice to know, thank you (even if I don't expect to use it: my goal was to be able to display the output of basic (primitive?) scheme function).
 
Thank you Harm for the information you added! And my apologies if my goal was not clear.
Best regards, g.

PNG image

PNG image


reply via email to

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