Created
November 9, 2015 17:58
-
-
Save davidnuon/84f44aecfd2c72074c86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Units where | |
data Feel = VeryCold | Cold | Nice | Hot | VeryHot deriving (Show, Eq, Ord) | |
data Temp = C Float | F Float | K Float deriving (Show, Eq) | |
-- Define C <-> F | |
ftoc (F t) = C $ (t - 32) * 5/9 | |
ctof (C t) = F $ (t * 9/5) + 32 | |
-- Define C <-> K | |
ctok (C t) = K $ t + 273.15 | |
ktoc (K t) = C $ t - 273.15 | |
-- Define F <-> K with F <-> C <-> K | |
ftok = ctok . ftoc | |
ktof = ctof . ktoc | |
-- Boiling | |
isFreezing :: Temp -> Bool | |
isFreezing (F t) | t <= 32 = True | |
| otherwise = False | |
isFreezing (C t) = isFreezing . ctof $ (C t) | |
isFreezing (K t) = isFreezing . ktof $ (K t) | |
-- Freezing | |
isBoiling :: Temp -> Bool | |
isBoiling (F t) | t >= 212 = True | |
| otherwise = False | |
isBoiling (C t) = isBoiling . ctof $ (C t) | |
isBoiling (K t) = isBoiling . ktof $ (K t) | |
(∈) x [u,v] = x >= u && x <= v | |
-- Heat Index | |
-- Being good Californians, we're using Faranheit as a rubric | |
feeling :: Temp -> Feel | |
feeling (F t) | t < 64 = VeryCold | |
| t < 70 = Cold | |
| t ∈ [70, 79] = Nice | |
| t > 100 = VeryHot | |
| t > 79 = Hot | |
feeling (C t) = feeling . ctof $ C t | |
feeling (K t) = feeling . ktof $ K t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment