Which do you suggest:
data Direction = Left | Right
type Direction = Bool
newtype Direction = Direction Bool
Then I’m making:
data Move = WalkRight Bool | Jump
or
data Move = Walk Direction | Jump
depending of the previous answer.
I have a function of type Char -> Maybe Move:
charToAction 'q' = Just $ WalkRight False
charToAction 'd' = Just $ WalkRight True
charToAction 'z' = Just Jump
charToAction _ = Nothing
Should I change my type Move into:
data Move = Stationary | WalkRight Bool | Jump
? The function would become:
charToAction 'q' = WalkRight False
charToAction 'd' = WalkRight True
charToAction 'z' = Jump
charToAction _ = Stationary
I wonder this because the list doesn’t need a Maybe:
data [a] = [] | a : [a]
Or is there a way to derive Maybe to make it cleaner?
I prefer something like:
under the principle that each data type clearly explains its own purpose, and doesn’t “assume” anything about how it might be used. Some counter-examples:
Here,
Directiondoesn’t really explain what it means… does it mean you have a direction or you don’t have a direction? Hmmm! Also, what happens when you needUpandDown? It’s hard to extend your program ifDirectionis just aBool.And
Movehas a data constructorStationaryhere that isn’t actually a move at all. You “leak” the notion that some keystrokes won’t result in a move. Again that will end up complicating your code.Make sense?