
I've been playing with my musical interval problem from before, and I got a little further, but I came up with a problem. I have two types-- Interval and BasicInterval. Interval has 12 constructors, Unison, MinorSecond, MajorSecond, MinorThird, et cetera. BasicInterval has 8 constructors, BasicUnison, Second, Third, Fourth, and so on. I want to be able to convert between them somewhat; I have function interToBasic, which at the moment looks like: interToBasic :: Interval -> BasicInterval interToBasic a = if (b == 1 || b == 2) then Second else if (b == 3 || b == 4) then Third .. where b = fromEnum a What I wanted to do, and figure is probably doable, but I can't seem to find it, is to say something like case (fromEnum a) of 1 || 2 -> Second 3 || 4 -> Third .. Is this doable? If so, what's the syntax?

On Thu, May 28, 2009 at 05:53:43PM -0400, Nathan Holden wrote:
interToBasic :: Interval -> BasicInterval interToBasic a = if (b == 1 || b == 2) then Second else if (b == 3 || b == 4) then Third .. where b = fromEnum a
What I wanted to do, and figure is probably doable, but I can't seem to find it, is to say something like
case (fromEnum a) of 1 || 2 -> Second 3 || 4 -> Third ..
Is this doable? If so, what's the syntax?
You can do this with pattern guards, like so: interToBasic a | b == 1 || b == 2 = Second | b == 3 || b == 4 = Third ... where b = fromEnum a You could also use b `elem` [1,2] as an alternative to b == 1 || b == 2 (especially nice when there are more than two options). -Brent

And I'm not sure what going via integers (via `fromEnum`) buys you. Why not: interToBasic :: Interval -> BasicInterval interToBasic a | a `elem` [MinorSecond, MajorSecond] = Second | a `elem` [MinorThird, MajorThird] = Third ... Dean At 6:27 PM -0400 5/28/09, Brent Yorgey wrote:
On Thu, May 28, 2009 at 05:53:43PM -0400, Nathan Holden wrote:
interToBasic :: Interval -> BasicInterval interToBasic a = if (b == 1 || b == 2) then Second else if (b == 3 || b == 4) then Third .. where b = fromEnum a
What I wanted to do, and figure is probably doable, but I can't seem to find it, is to say something like
case (fromEnum a) of 1 || 2 -> Second 3 || 4 -> Third ..
Is this doable? If so, what's the syntax?
You can do this with pattern guards, like so:
interToBasic a | b == 1 || b == 2 = Second | b == 3 || b == 4 = Third ... where b = fromEnum a
You could also use b `elem` [1,2] as an alternative to b == 1 || b == 2 (especially nice when there are more than two options).
-Brent

Hi Nathan, do you mean something like the following? data Interval = Unison | MinorSecond | MajorSecond | MinorThird | MajorThird deriving (Show) data BasicInterval = BasicUnison | Second | Third | Fourth deriving (Show) inter2basic :: Interval -> BasicInterval inter2basic x = case x of Unison -> BasicUnison MinorSecond -> Second MajorSecond -> Second MinorThird -> Third MajorThird -> Third Happy Hacking, Thomas Nathan Holden wrote:
I've been playing with my musical interval problem from before, and I got a little further, but I came up with a problem.
I have two types-- Interval and BasicInterval.
Interval has 12 constructors, Unison, MinorSecond, MajorSecond, MinorThird, et cetera. BasicInterval has 8 constructors, BasicUnison, Second, Third, Fourth, and so on.
I want to be able to convert between them somewhat;
I have function interToBasic, which at the moment looks like:
interToBasic :: Interval -> BasicInterval interToBasic a = if (b == 1 || b == 2) then Second else if (b == 3 || b == 4) then Third .. where b = fromEnum a
What I wanted to do, and figure is probably doable, but I can't seem to find it, is to say something like
case (fromEnum a) of 1 || 2 -> Second 3 || 4 -> Third ..
Is this doable? If so, what's the syntax? ------------------------------------------------------------------------
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Yes. I did. In fact, that's what it looks like. But in general I also derive
Enum, and use Ints instead, because that's how I conveniently switch between
notes and intervals.
On Fri, May 29, 2009 at 10:27 AM, Thomas Friedrich
Hi Nathan,
do you mean something like the following?
data Interval = Unison | MinorSecond | MajorSecond | MinorThird | MajorThird deriving (Show)
data BasicInterval = BasicUnison | Second | Third | Fourth deriving (Show)
inter2basic :: Interval -> BasicInterval inter2basic x = case x of Unison -> BasicUnison MinorSecond -> Second MajorSecond -> Second MinorThird -> Third MajorThird -> Third
Happy Hacking, Thomas
Nathan Holden wrote:
I've been playing with my musical interval problem from before, and I got a little further, but I came up with a problem.
I have two types-- Interval and BasicInterval.
Interval has 12 constructors, Unison, MinorSecond, MajorSecond, MinorThird, et cetera. BasicInterval has 8 constructors, BasicUnison, Second, Third, Fourth, and so on.
I want to be able to convert between them somewhat;
I have function interToBasic, which at the moment looks like:
interToBasic :: Interval -> BasicInterval interToBasic a = if (b == 1 || b == 2) then Second else if (b == 3 || b == 4) then Third .. where b = fromEnum a
What I wanted to do, and figure is probably doable, but I can't seem to find it, is to say something like
case (fromEnum a) of 1 || 2 -> Second 3 || 4 -> Third ..
Is this doable? If so, what's the syntax? ------------------------------------------------------------------------
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Brent Yorgey
-
Dean Herington
-
Nathan Holden
-
Thomas Friedrich