-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnits.hs
More file actions
37 lines (29 loc) · 1006 Bytes
/
Copy pathUnits.hs
File metadata and controls
37 lines (29 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Units
(
MetricUnit(..),
ImperialUnit(..),
Measurement(..),
convert
)
where
data MetricUnit = Meter | Liter | KiloGram deriving (Show, Eq)
data ImperialUnit = Yard | Gallon | Pound deriving (Show, Eq)
data Measurement = MetricMeasurement Double MetricUnit | ImperialMeasurement Double ImperialUnit deriving (Show, Eq)
symbol1 :: MetricUnit -> String
symbol1 Meter = "m"
symbol1 Liter = "L"
symbol1 KiloGram = "kg"
symbol2 :: MetricUnit -> String
symbol2 x
| x == Meter = "m"
| x == Liter = "L"
| x == KiloGram = "kg"
convert :: Measurement -> Measurement
convert (MetricMeasurement x u)
| u == Meter = ImperialMeasurement (1.0936*x) Yard
| u == Liter = ImperialMeasurement (0.2642*x) Gallon
| u == KiloGram = ImperialMeasurement (2.2046*x) Pound
convert (ImperialMeasurement x u)
| u == Yard = MetricMeasurement (0.9144*x) Meter
| u == Gallon = MetricMeasurement (3.7854*x) Liter
| u == Pound = MetricMeasurement (0.4536*x) KiloGram