lilypond-user
[Top][All Lists]
Advanced

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

Re: scheme set list function


From: Gianmaria Lari
Subject: Re: scheme set list function
Date: Mon, 8 Apr 2019 10:05:53 +0200

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.
For that I thought to use the following three functions:

#(define (incNumber l) (append (drop-right l 1) (list (1+ (last l)))))
#(define (unindent l) (drop-right l 1))
#(define (indent l) (append l '(1)))

The state of the numbering (the fact that I'm at 1.1 or 3.1.1) is kept in a list that the three functions modify.

In an object oriented approach I would create a class with these three functions and the data structure (list or what else) on which they work on.

In scheme I'm not able to (easily) encapsulate the functions and data in a single element so I thought to create the three functions and pass to them the list on which they have to operate. The functions *need* to change the list. Of course I could do it manually externally to the functions but this increase the risk of introducing bug in the code. The following is just an example of how I solve my problem with the external set! (that I don't think is the right solution for this problem):

\version "2.21.0"
#(define (incNumber l) (append (drop-right l 1) (list (1+ (last l)))))
#(define (unindent l) (drop-right l 1))
#(define (indent l) (append l '(1)))

#(define mylist '(1))
#(display mylist) #(newline) %output: (1)

#(set! mylist (incNumber mylist))
#(display mylist)#(newline) %output: (2)
#(set! mylist (incNumber mylist))
#(display mylist)#(newline) %output: (3)

#(set! mylist (indent mylist))
#(display mylist)#(newline) %output: (3 1)

#(set! mylist (incNumber mylist))
#(display mylist)#(newline) %output: (3 2)

#(set! mylist (incNumber (unindent mylist)))
#(display mylist)#(newline) %output: (4)


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

* *

If I correctly understood, the macro could be another solution. But I have no idea if is the "right" (good) solution (and well, the syntax is to me a bit intimidating).

* *

Finally I tried to use drop-right! and append! in my functions like this: 

#(define (unindent! l) (drop-right! l 1))
#(define (indent! l) (append! l '(1)))

and this works. But I have no idea how to do the same with 

#(define (incNumber l) (append (drop-right l 1) (list (1+ (last l)))))

(I tried to use set! on the left of (append.......) but it doesn't work.
Thank you for your help.
g.

P.S. Another possibility that I didn't yet tried: if the scheme list is a structure made of couple of information, data + pointer,  maybe I could put my information starting from the second element of the list..... sorry if I'm not very clear. I will try and then I will try to explain myself better.

P.P.S 
To Andrew:
Yes Andrew, you're right. Instead of solve my problem by trial and error it would be better to read a good scheme book. I already started and stopped many times...... But I have some books on my (virtual) desktop that I will really read before or later
To Aaron: thank you for having reminded me the convention of exclamation point! I used it in my last code!

reply via email to

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