check undefined variable scheme

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

check undefined variable scheme

Gianmaria Lari
The following function increase a counter by 1 and return it as string

#(define count 0) 
#(define (nextcount) (begin
                      (set! count (+ 1 count))
                      (number->string count)
                      )
   )

Is my code ok, or I should write it in a different way?
Is it possible to define "count" inside the function just in case it is undefined?

Thank you, g.

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: check undefined variable scheme

Urs Liska-3



Am 24.05.2018 um 09:56 schrieb Gianmaria Lari:
The following function increase a counter by 1 and return it as string

#(define count 0) 
#(define (nextcount) (begin
                      (set! count (+ 1 count))
                      (number->string count)
                      )
   )

Is my code ok, or I should write it in a different way?

Two observations:
- You don't need that (begin ...) wrapper because the procedure definition already behaves as such
- You shouldn't use reserved words for variable names, so I'd use 'counter' instead.
Is it possible to define "count" inside the function just in case it is undefined?

Yes, see:

\version "2.19.80"

#(define (nextcount)
   (if (not (defined? 'counter)) (ly:parser-define! 'counter 0))
   (set! counter (+ 1 counter))
   (number->string counter)
   )


#(ly:message "~a" (nextcount))
#(ly:message "~a" (nextcount))

HTH
Urs

Thank you, g.


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: check undefined variable scheme

David Kastrup
Urs Liska <[hidden email]> writes:

> Am 24.05.2018 um 09:56 schrieb Gianmaria Lari:
>> The following function increase a counter by 1 and return it as string
>>
>>     #(define count 0)
>>     #(define (nextcount) (begin
>>                           (set! count (+ 1 count))
>>                           (number->string count)
>>                           )
>>        )
>>
>> Is my code ok, or I should write it in a different way?
>
> Two observations:
> - You don't need that (begin ...) wrapper because the procedure
> definition already behaves as such
> - You shouldn't use reserved words for variable names, so I'd use
> 'counter' instead.
>> Is it possible to define "count" inside the function just in case it
>> is undefined?
>
> Yes, see:
>
> \version "2.19.80"
>
> #(define (nextcount)
>    (if (not (defined? 'counter)) (ly:parser-define! 'counter 0))
>    (set! counter (+ 1 counter))
>    (number->string counter)
>    )

You actually don't need to define it externally:

    #(define nextcount
      (let ((counter 0))
       (lambda ()
        (set! counter (1+ counter))
        (number->string counter))))

    $(nextcount)

    $(nextcount)

will work just fine.


--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: check undefined variable scheme

Urs Liska-3


Am 24.05.2018 um 10:46 schrieb David Kastrup:

> Urs Liska <[hidden email]> writes:
>
>> Am 24.05.2018 um 09:56 schrieb Gianmaria Lari:
>>> The following function increase a counter by 1 and return it as string
>>>
>>>      #(define count 0)
>>>      #(define (nextcount) (begin
>>>                            (set! count (+ 1 count))
>>>                            (number->string count)
>>>                            )
>>>         )
>>>
>>> Is my code ok, or I should write it in a different way?
>> Two observations:
>> - You don't need that (begin ...) wrapper because the procedure
>> definition already behaves as such
>> - You shouldn't use reserved words for variable names, so I'd use
>> 'counter' instead.
>>> Is it possible to define "count" inside the function just in case it
>>> is undefined?
>> Yes, see:
>>
>> \version "2.19.80"
>>
>> #(define (nextcount)
>>     (if (not (defined? 'counter)) (ly:parser-define! 'counter 0))
>>     (set! counter (+ 1 counter))
>>     (number->string counter)
>>     )
> You actually don't need to define it externally:
>
>      #(define nextcount
>        (let ((counter 0))
>         (lambda ()
>          (set! counter (1+ counter))
>          (number->string counter))))
>
>      $(nextcount)
>
>      $(nextcount)
>
> will work just fine.
>
>

Ah yes, that's more idiomatic and (thus) elegant.
Gianmaria, if you want to understand what's happening there read
https://www.gnu.org/software/guile/docs/docs-1.8/guile-ref/Closure.html#Closure


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: check undefined variable scheme

Andrew Bernard
In reply to this post by Gianmaria Lari
Hi Gianmaria,

The others have beat me to it, but I just wanted to say you should read up on closures in Scheme. This will give you are really good grasp of what's going on.


Andrew



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: check undefined variable scheme

Gianmaria Lari
Thank you David, Urs and Andrew for the code, the link to the documentation and the study suggestion!

On Thu, 24 May 2018 at 11:00, Andrew Bernard <[hidden email]> wrote:
Hi Gianmaria,

The others have beat me to it, but I just wanted to say you should read up on closures in Scheme. This will give you are really good grasp of what's going on.


Andrew



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user