Coloring notes in Lilypond by pitch - lilypond

lilypond can color notes in a arbitrary way using
\override NoteHead #'color = #red c
with the default color is black. But I like to color all notes by pitch, so that my kids can more easily learn to recognize the notes as the c, d, e, f, ... are associated with its own color. The above allows me to do this, but is rather verbose.
Is there a shortcut, macros of some sort, that allow me to do something along the lines of:
redc greend bluee
or even overwriting the default colors for each note by pitch so that I can even simply do:
c d e
and have each of them have a different color?

There is an example for this in the snippets:
%Association list of pitches to colors.
#(define color-mapping
(list
(cons (ly:make-pitch 0 0 0) (x11-color 'red))
(cons (ly:make-pitch 0 0 1/2) (x11-color 'green))
(cons (ly:make-pitch 0 1 -1/2) (x11-color 'green))
(cons (ly:make-pitch 0 2 0) (x11-color 'red))
(cons (ly:make-pitch 0 2 1/2) (x11-color 'green))
(cons (ly:make-pitch 0 3 -1/2) (x11-color 'red))
(cons (ly:make-pitch 0 3 0) (x11-color 'green))
(cons (ly:make-pitch 0 4 1/2) (x11-color 'red))
(cons (ly:make-pitch 0 5 0) (x11-color 'green))
(cons (ly:make-pitch 0 5 -1/2) (x11-color 'red))
(cons (ly:make-pitch 0 6 1/2) (x11-color 'red))
(cons (ly:make-pitch 0 1 0) (x11-color 'blue))
(cons (ly:make-pitch 0 3 1/2) (x11-color 'blue))
(cons (ly:make-pitch 0 4 -1/2) (x11-color 'blue))
(cons (ly:make-pitch 0 5 1/2) (x11-color 'blue))
(cons (ly:make-pitch 0 6 -1/2) (x11-color 'blue))
))
%Compare pitch and alteration (not octave).
#(define (pitch-equals? p1 p2)
(and
(= (ly:pitch-alteration p1) (ly:pitch-alteration p2))
(= (ly:pitch-notename p1) (ly:pitch-notename p2))))
#(define (pitch-to-color pitch)
(let ((color (assoc pitch color-mapping pitch-equals?)))
(if color
(cdr color))))
#(define (color-notehead grob)
(pitch-to-color
(ly:event-property (ly:grob-property grob 'cause) 'pitch)))
\score {
\new Staff \relative c' {
\override NoteHead #'color = #color-notehead
c8 b d dis ees f g aes
}
}

There's a question It is possible to color note heads depending on their pitch? at the LilyPond Snippet Repository. You get the answer by clicking on the stave.

OK, for the book Kid's Keyboard Course - Book #1 I bought earlier this year in Cambridge, I now have this color coding:
#(define color-mapping
(list
(cons (ly:make-pitch 0 0 0) (x11-color 'magenta))
(cons (ly:make-pitch 0 1 -1/2) (x11-color 'grey))
(cons (ly:make-pitch 0 1 0) (x11-color 'grey))
(cons (ly:make-pitch 0 1 1/2) (x11-color 'grey))
(cons (ly:make-pitch 0 2 0) (x11-color 'red))
(cons (ly:make-pitch 0 2 1/2) (x11-color 'red))
(cons (ly:make-pitch 0 3 -1/2) (x11-color 'green))
(cons (ly:make-pitch 0 3 0) (x11-color 'green))
(cons (ly:make-pitch 0 4 -1/2) (x11-color 'blue))
(cons (ly:make-pitch 0 4 0) (x11-color 'blue))
(cons (ly:make-pitch 0 4 1/2) (x11-color 'blue))
(cons (ly:make-pitch 0 5 0) (x11-color 'yellow))
(cons (ly:make-pitch 0 5 -1/2) (x11-color 'yellow))
(cons (ly:make-pitch 0 5 1/2) (x11-color 'yellow))
(cons (ly:make-pitch 0 6 1/2) (x11-color 'purple))
(cons (ly:make-pitch 0 6 0) (x11-color 'purple))
(cons (ly:make-pitch 0 6 -1/2) (x11-color 'purple))))

Related

Why my Racket program does not give me any output?

I have this code written for converting measurements. However when I run it with command (fce cm mm 5) I don't see any output and any error.
#lang racket
(define eq equal?)
(define (fce jednotka jednotka2 cislo)
(cond
((eq jednotka "mm") (mm cislo jednotka2))
((eq jednotka "cm") (cm cislo jednotka2))
((eq jednotka "m") (m cislo jednotka2))
((eq jednotka "km") (km cislo jednotka2))))
(define (mm c j)
(cond
((eq j "cm") (format "~a mm = ~a ~a" c (* c 0.1) j))
((eq j "m") (format "~a mm = ~a ~a" c (* c 0.001) j))
((eq j "km") (format "~a mm = ~a ~a" c (* c 0.000001) j))))
(define (cm c j)
(cond
((eq j "mm") (format "~a mm = ~a ~a" c (* c 0.1) j))
((eq j "m") (format "~a mm = ~a ~a" c (* c 0.001) j))
((eq j "km") (format "~a mm = ~a ~a" c (* c 0.000001) j))))
(define (m c j)
(cond
((eq j "cm") (format "~a mm = ~a ~a" c (* c 0.1) j))
((eq j "mm") (format "~a mm = ~a ~a" c (* c 0.001) j))
((eq j "km") (format "~a mm = ~a ~a" c (* c 0.000001) j))))
(define (km c j)
(cond
((eq j "cm") (format "~a mm = ~a ~a" c (* c 0.1) j))
((eq j "m") (format "~a mm = ~a ~a" c (* c 0.001) j))
((eq j "km") (format "~a mm = ~a ~a" c (* c 0.000001) j))))
You forgot to quote the strings, and you're comparing procedures. You should do this instead:
(fce "cm" "mm" 5)
The logic doesn't look right, though. The above prints:
"5 mm = 0.5 mm"
You need to work out the proper conversions and displayed messages in each of the helper procedures.

Solving inequality leaves CRootOf in answer

when I try to solve this inequality x^3 + 3*x > 3 I get this output:
octave:5> eqn = x^3 + 3*x > 3
eqn = (sym)
3
x + 3⋅x > 3
octave:6> solve(eqn, x)
ans = (sym)
⎛ 3 ⎞
x < ∞ ∧ CRootOf⎝x + 3⋅x - 3, 0⎠ < x
But when I try to solve simpler inequality everything works fine:
eqn = abs(x^2-3)>3
eqn = (sym)
│ 2 │
│x - 3│ > 3
octave:4> solve(eqn, x)
ans = (sym) (-∞ < x ∧ x < -√6) ∨ (√6 < x ∧ x < ∞)
What can I do with this CRoot?

Haskell Instances required for definition

I have the following Haskell code, a function that checks how many digits are the same in a number and based on how many of them were the same takes a value. I keep getting the error:
Instances of (Ord (Int -> Int -> Int), Num (Int -> Int -> Int)) required for definition of digits
digits :: Int->Int->Int
digits x y
|(equal < 2) = 0
|(equal == 2) = 1
|(equal == 3) = 5
|(equal == 4) = 20
|(equal == 5) = 300
|(equal == 6) = 8000
|(equal == 7) = 10000
|otherwise = 100000
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 1 + equal (a `div` 10) (b `div` 10)
What am I doing wrong? Also what does the error mean exactly?
EDIT: Did it like this and it works like a charm!
digits :: Int->Int->Int
digits x y
|(c < 2) = 0
|(c == 2) = 1
|(c == 3) = 5
|(c == 4) = 20
|(c == 5) = 300
|(c == 6) = 8000
|(c == 7) = 10000
|otherwise = 100000
where c = equal x y
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 0 + equal (a `div` 10) (b `div` 10)

Haskell program simple game

I am very new in haskell-programming. I try to program a simple dice-game, but I don't know to do it in haskell.
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a₁,a₂,a₃) c (d₁,d₂,d₃)
I want to consider each difference dᵢ - aᵢ (but not if aᵢ > dᵢ) and return False if (d1-a1)+(d2-a2)+(d3-a3) are larger than c. (if aᵢ > dᵢ then I sum up 0 instead the difference)
I try something like this:
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) = ?????
where diff1 = if (d1 > a1) then d1-a1
diff2 = if (d2 > a2) then d2-a2
diff3 = if (d3 > a3) then d3-a3
Because in Haskell, else is not a optional part of an if expression, so you need to define diff1 as diff1 = if d1 > a1 then d1 - a1 else 0. Other two are similar.
Notes that > returns a Bool value, so you could just sum these three differences up and compare it with c, and use it as your condition.
There are several ways to define this function:
suc1 (a1, a2, a3) c (d1, d2, d3) = diff1 + diff2 + diff3 <= c
where diff1 = if d1 > a1 then d1 - a1 else 0
diff2 = if d2 > a2 then d2 - a2 else 0
diff3 = if d3 > a3 then d3 - a3 else 0
suc2 (a1, a2, a3) c (d1, d2, d3) = sum diffs <= c
where diff1 = max (d1-a1) 0
diff2 = max (d2-a2) 0
diff3 = max (d3-a3) 0
diffs = [diff1, diff2, diff3]
suc3 (a1, a2, a3) c (d1, d2, d3) = sum (zipWith diff as ds) <= c
where diff a d = max (d-a) 0
as = [a1, a2, a3]
ds = [d1, d2, d3]
How about this?
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) =
((if a1> d1 then 0 else d1-a1) + (if a2> d2 then 0 else d2-a2) + (if a3>d3 then 0 else d3-a3) > c )
Or alternatively
suc :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
suc (a1, a2, a3) c (d1, d2, d3) =
max 0 (d1-a1) + max 0 (d2-a2) + max 0 (d3-a3) > c

Excel VBA UDF Formatting - Change argument values before calculation

I am trying to write a formula in Excel VBA to calculate: RR = ((A / (A + B)) / (C / (C + D)))
When any of the four arguments (A, B, C, D) are 0, I want to change their value to 0.5 in the calculation.
Is there an easy way of doing this? I believe my formatting is wrong or I'm going about it the wrong way. Any helpful tips would be greatly appreciated.
I've tried:
Function RR(A, B, C, D) As Double
If A = 0 And B = 0 Then
A = 0.5
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 Then
A = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf B = 0 Then
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf C = 0 Then
C = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf D = 0 Then
D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 And D = 0 Then
A = 0.5 And D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
Else: RR = ((A / (A + B)) / (C / (C + D)))
End If
End Function
Try this one:
Function RR(A, B, C, D) As Double
If A = 0 Then A = 0.5
If B = 0 Then B = 0.5
If C = 0 Then C = 0.5
If D = 0 Then D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
End Function