lilypond-user
[Top][All Lists]
Advanced

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

Re: scheme set list function


From: David Kastrup
Subject: Re: scheme set list function
Date: Mon, 08 Apr 2019 13:35:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Aaron Hill <address@hidden> writes:

> On 2019-04-08 2:48 am, Thomas Morley wrote:
>> foo =
>> #(let ((x (cons 1 0)))
>>   (define-scheme-function (arg)(symbol?)
>>     (case arg
>>       ((indent) (set! x (cons (car x) (1+ (cdr x)))))
>>       ((increase) (set! x (cons (1+ (car x)) 0)))
>>       ((reset) (set! x (cons 1 0))))
>>     (if (zero? (cdr x))
>>         (format #f "~a" (car x))
>>         (format #f "~a.~a" (car x) (cdr x)))))
>>
>> [ . . . ]
>>
>> I'm still not happy with those set-whatever!-thingies. I was beaten
>> too often. Maybe someone comes up with a better approach.
>
> Using set! is perfectly fine as long as you encapsulate things well.
> Your use of let to define a local variable minimizes the chance that
> folks would be able to interfere with or even care about such
> modification.  However, your usage means there is still a "global" x
> that is shared amongst all of the usage of foo.

There is no global variable x.  There is a binding, but the scope of the
binding ends with the let.  This binding is anonymous afterwards and has
no name.  You can call functions manipulating the global variable x from
inside of foo and their action is not related to having used the binding
x when defining foo.

-- 
David Kastrup



reply via email to

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