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: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 49:59:22 |
Calls: | 2,470 |
Calls today: | 1 |
Files: | 8,638 |
Messages: | 1,896,807 |