[Haskell-beginners] Intervals?
aditya siram
aditya.siram at gmail.com
Tue May 19 15:43:04 EDT 2009
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 <nathanmholden at gmail.com>wrote:
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090519/0cb9f5fa/attachment.html
More information about the Beginners
mailing list