lilypond-user
[Top][All Lists]
Advanced

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

Re: scheme set list function


From: Carl Sorensen
Subject: Re: scheme set list function
Date: Mon, 8 Apr 2019 13:05:27 +0000
User-agent: Microsoft-MacOutlook/10.10.7.190210


On 4/8/19, 3:48 AM, "Thomas Morley" <address@hidden> wrote:

    Am Mo., 8. Apr. 2019 um 10:07 Uhr schrieb Gianmaria Lari
    <address@hidden>:
    >
    > I try to explain what I'm trying to do.
    >
    > I want some functions helping me to numbering exercise like this:
    >
    > 1
    > 2
    > 3
    > 3.1
    > 3.2
    > 3.3
    > 4
    > 4.1
    > 4.2
    >
    > etc.
    
    > I thought that an alternative approach that looks nice to try but it's 
over-engineering would be to create a single function able to do the work of 
the three functions according to a parameter. Something like
    >
    >
    > #(define (indenter operation l) .....)
    >
    >
    > if the "operation" argument is "inc" the indenter function has to inc, if 
the "operation" argument is "indent" the indenter function has to indent etc. I 
hope I have been clear.
    > This solution is nice because encapsulate all - data and structure - and 
has no problem to modify the list that would be a local persistent data (I 
don't remember how you call it in scheme).
    >
    
    How about:
    
    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)))))
    
    \markup \foo #'start
    \markup \foo #'indent
    \markup \foo #'indent
    \markup \foo #'indent
    \markup \foo #'increase
    \markup \foo #'indent
    \markup \foo #'indent
    \markup \foo #'indent
    
    
    \markup \foo #'reset
    \markup \foo #'indent
    \markup \foo #'increase
    \markup \foo #'indent
    
    
    I'm still not happy with those set-whatever!-thingies. I was beaten
    too often. Maybe someone comes up with a better approach.

This is pretty consistent with the way objects with local state are implemented 
in Structure and Interpretation of Computer Programs.

See https://web.mit.edu/alexmv/6.037/sicp.pdf and check out the bank account 
examples in section 3.1

But you haven't implemented 'start in this code, have you?    It's a default 
no-op.  You could start your indentation by using any symbol that is not 
'reset, 'increase, or 'indent, iIUC.


HTH,

Carl
 


reply via email to

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