Hi Gianmaria,

 

Your requirement is to have a numbered list, similar to that in outlines. 
Basically what you want is a counter, and this can be implemented in Lisp like 
languages with a closure. A closure results from the fact that in Scheme a 
function records its environment, and this includes variables in scope. Scheme 
variables have ‘infinite extent’ and so are kept forever as long as there is a 
function that references them. I am not going to give a tutorial on closures 
here, but I will give you an indication.

 

Functional programming shows you that you need to think about ‘state’ very 
carefully. I won’t go into that here. But a closure effectively produces a 
function with private state, so a counter for example can remember its value 
between successive invocations. You want a counter that can remember its value 
and its ‘indent level’.

 

Here’s a counter that can take an initial value. This is Scheme, not lilypond. 
Use the guile REPL to try it out.

 

(define make-counter-init

  (lambda (count)

    (lambda ()

      (let ((val count))

                (set! count (+ count 1))

                val

                ))))

 

 

This returns a procedure (the lambda) that counts:

 

(define n1 (make-counter-init 1))

 

then:

 

(n1)

1

(n1)

2

 

But the main point is that instances are independent and contain their own 
state:

 

(define n2 (make-counter-init 100))

 

(n2)

100

(n2)

101

(n1)

3

(n1)

4

(n2)

102

 

Obviously it is trivial to write something like this that converts numbers to 
formatted strings. I did not show that in the interests of clarity.

 

I am not going to give a lecture on closures here – that’s an exercise for you.

 

My concept is to elaborate this so that part of the state is the indent level, 
and we can provide optional arguments to indent and outdent and even ‘reset’ 
(aha!_ the counter. I will work that up for you. It’s actually a very 
interesting programming exercise and would make a good student task.

 

 

Andrew

 

 

 

 

_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to