Recently I wanted a type that adds another alternative to Maybe,
specifically an approximate result, so I wrote (somewhat without
thinking)
data Approximate a = Roughly a | Maybe a
Haskell does not complain about the type (after all, it just looks like
I'm defining a constructor called Maybe)
I can nest the Maybe in a new type:
data Approximate a = Roughly a | Exactly (Maybe a)
f a | a < 0 = Exactly Nothing
| a > 10000 = Roughly (sqrt a)
| otherwise = Exactly (Just (a / 2))
but I can't help wondering if I'm missing a neater way to do this.
data Precision a = Roughly a | Exactly a
data Approximate a = Maybe (Precision a)
data Approximate a = Roughly a | Exactly (Maybe a)
f a | a < 0 = Exactly Nothing
| a > 10000 = Roughly (sqrt a)
| otherwise = Exactly (Just (a / 2))
but I can't help wondering if I'm missing a neater way to do this.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 185 |
Nodes: | 16 (1 / 15) |
Uptime: | 83:13:37 |
Calls: | 3,750 |
Files: | 11,172 |
Messages: | 3,461,816 |