sharps
use accidentals_count($accidentals, $accidentals_count)
when
$accidentals_count = 0
notany
'b' in $accidentals
$accidentals_count = $accidentals_count + $accidentals.__len__()
I have also another rule which is the same as this, but inside the
"notany" has '#' in $accidentals and the len should be multiplied by
"-1", this way: ($accidentals.__len__() * (-1))
$accidentals should contain a certain number of 'b' or '#'. I mean, it
may be: 'bbbb', or '##', '#######', but not 'b##'.
So I want to check that they are all the same and in the end I need the count.
I think I'm doing something wrong with the notany.
Any suggestion?
Thanks,
Carlo
Thanks.
I think I cannot use the "count" method, because $accidentals is a tuple.
It's a tuple because in the top level rule I have a string like this:
C## (that may have 0, 1 or more #), then I divide it into a tuple and
put the "C" in a variable and the rest in *$accidentals.
Do you suggest to transform it again in a list or something, or shall
I just use the __len__() method?
>
> sharps
> use accidentals_count($accidentals, $accidentals_count)
> when
> $accidentals_count = $accidentals.count('#')
> check $accidentals_count == len($accidentals)
Great, this is very clever :D
> For the flats, I'd store the count of the 'b' characters in a variable other
> than $accidentals_count (say $b_count) and then have:
>
> $accidentals_count = -$b_count
>
> at the end of the rule (after the check).
>
> If you use $accidentals_count for the $b_count, then you won't be able to
> change it's value at the end to negate it. Rather, the rule will fail
> (unless the count is zero), because the result of the negation will not
> match the value already assigned to $accidentals_count. What happens is
> that the value computed by Python on the right side of the '=' is pattern
> matched against the pattern on the left side. If the left side is an
> unbound variable, the variable is bound to the value. If the left side is a
> bound variable, then the two values must match for the line to succeed.
I think I'll need some time to get this, but thanks a lot.
Bye,
Carlo
2009/10/27 Bruce Frederiksen <dang...@gmail.com>:
> Hi Carlo!Thanks.
>
> Good to see you making progress with your music program on Pyke!
>
> To answer your question, Python has a nice 'count' method on strings:
I think I cannot use the "count" method, because $accidentals is a tuple.
It's a tuple because in the top level rule I have a string like this:
C## (that may have 0, 1 or more #), then I divide it into a tuple and
put the "C" in a variable and the rest in *$accidentals.
Do you suggest to transform it again in a list or something, or shall
I just use the __len__() method?
Yes, I solved with this.
Anyway, there's a problem with this accidental_count. I didn't see it
before, but in the way I wrote the rules, when I try to prove the goal
from the top level rule it just tries to prove it with the first rule
in the code. This is because they have the same "use" line, which is:
use accidentals_count($accidentals, $accidentals_count)
I thought I could write that like this:
use accidentals_count(('b', *$accidentals), $accidentals, $accidentals_count)
and
use accidentals_count(('#', *$accidentals), $accidentals, $accidentals_count)
but it doesn't seem to work. I thought it should work, because I call
it in this way:
accidentals_count($accidentals, $accidentals_count)
and at this point $accidentals is this: ('#', '#')
Ok, one error is here, because it should be something like:
accidentals_count($accidentals, $acc, $accidentals_count)
Still there's something wrong. The trick of putting the '#' or 'b' to
have a match removes an element, so the count is wrong. I think I can
just add one, but maybe there's something else.