
I have been trying to optimize some research I was doing in my spare time. To spare complexity, it involves music-- I have a class, defining each note in an octave, and I want to write a function that will convert between two notes and an interval-- so, for instance: Foo As {- A sharp-- can't use # -} B = 1 because in the standard scale, A# and B are one semitone apart. My question, however, boils primarily down to this: Is there a way to make Foo :: Note -> Note -> Int Foo a b = a-b work?

Excerpts from nathanmholden's message of Tue May 19 14:43:04 -0400 2009:
My question, however, boils primarily down to this: Is there a way to make
Foo :: Note -> Note -> Int Foo a b = a-b
A sensible implementation of the Num typeclass for Note should do the trick nicely. Cheers, Edward

How about something like this?
import Data.List
data Note = C | Cs | D | Eb | E | F | Fs | G | Ab | A | Bb | B deriving Enum
--Counting forward from n1 to n2
interval :: Note -> Note -> Int
n1 `interval` n2 = (12 - fromEnum n1) + fromEnum n2
--Counting back from n2 to n1
bInterval :: Note -> Note -> Int
n1 `bInterval` n2 = fromEnum n1 - fromEnum n2
testInterval = A `interval` D
testBInterval = A `bInterval` D
I don't see how these operations could be made part of the Num because the
output of the Num class has to be the same as the input. For instance, when
adding two numbers, the output is a number, but for determining intervals,
the inputs are Notes and the output is a number.
-deech
On Tue, May 19, 2009 at 1:43 PM, Nathan Holden
I have been trying to optimize some research I was doing in my spare time.
To spare complexity, it involves music-- I have a class, defining each note in an octave, and I want to write a function that will convert between two notes and an interval-- so, for instance:
Foo As {- A sharp-- can't use # -} B = 1
because in the standard scale, A# and B are one semitone apart.
My question, however, boils primarily down to this: Is there a way to make
Foo :: Note -> Note -> Int Foo a b = a-b
work?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
aditya siram
-
Edward Z. Yang
-
Nathan Holden