Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

haskell - Is there any chance to write "C major" instead of "major C"?

I encountered a small aesthetic issue in my music project and it has been bugging me for some time.

I have a type data Key = C | D | ... and I can construct a Scale from a Key and a Mode. The Mode distinguishes between e.g. a major and a minor scale.

I can define the Mode type as a function from Key to Scale. In that case the modes will have lowercase names (which is fine) and I can get a Scale like this

aScale = major C

But musicians don't talk like this. They refer to this scale as the C major scale, not the major C scale.

What I want

Ideally I'd want to write

aScale = C major

Is this possible at all?

What I tried

I can make Key a function that constructs a Scale from a Mode, so I can write

aScale = c Major

But I cannot confine Keys to constructing Scales. They are needed for other things as well (e.g. constructing chords). Also Key should be an instance of Show.


I can put the Mode after the Key when I use an extra function (or value constructor):

aScale = scale C major with scale :: Key -> Mode -> Scale

But the extra word scale looks noisy and contrary to its name, scale isn't really concerned with scales. The intelligent part is in major, scale is really just flip ($).


Using a newtype Mode = Major | Minor ... doesn't really change much, except scale needs to be more intelligent:

aScale = scale C Major
question from:https://stackoverflow.com/questions/60002268/is-there-any-chance-to-write-c-major-instead-of-major-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution 1:

Use this

data Mode  = Major | Minor
data Scale = C Mode | D Mode | E Mode | F Mode | G Mode | A Mode | B Mode 

Now you can write (with capital C and capital M)

aScale = C Major

Solution 2a:

This is also possible

data Mode  = Major | Minor
data Key   = C | D | E | F | G | A | B 

data Scale = Scale Key Mode  

Now you write

aScale = Scale C Major

Solution 2b:

This is also possible

data Mode  = Major | Minor
data Key   = C | D | E | F | G | A | B 

type Scale = (Key, Mode)  

Now you write

aScale = (C, Major)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...