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: | 286 |
Nodes: | 16 (2 / 14) |
Uptime: | 83:51:14 |
Calls: | 6,495 |
Calls today: | 6 |
Files: | 12,096 |
Messages: | 5,276,885 |