|
From: | Gianmaria Lari |
Subject: | Re: output some string |
Date: | Sun, 17 Jun 2018 10:32:14 +0200 |
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,
HarmThank 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.
\version "2.19.81"$(write "This is a test\n")$(write '(alfa beta gamma))
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:Should be noted that,is equivalent to$(object->string 3)$(object->string 3 display)Nope. Default printing-procedure is `write`, see:$(object->string "3")
$(object->string "3" display)
$(object->string "3" write)
* * *Some examples displaying a lilypond type, cis1. Please note the code is always the same except the optional argument.(the output in the previous example is cut on the right!!)\version "2.19.81"$(object->string #{ cis1 #}) %no optional argumentWhat did you expect?
Console would usually break the lines of a long string. LilyPond only, if you tell her:$(make-wordwrap-string-markup (object->string #{ cis1 #}))
[Prev in Thread] | Current Thread | [Next in Thread] |