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 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