Skip to content

Instantly share code, notes, and snippets.

@jessearmand
Last active December 30, 2015 05:29
Show Gist options
  • Save jessearmand/7783275 to your computer and use it in GitHub Desktop.
Save jessearmand/7783275 to your computer and use it in GitHub Desktop.
A simple haskell module to calculate utilties cost.
module UtilitiesCost
( electricityCost
, gasCost
, waterCost
, utilitiesCost
) where
import Debug.Trace
data Reading = Reading Double Double deriving (Eq, Show, Read)
data ReadingRange = ReadingRange (Double, Double) deriving (Eq, Show, Read)
data WaterExtraCost = WaterExtraCost Double Double Double deriving (Eq, Show, Read)
electricityCost :: Reading -> Double -> Double -> Double -> Double
electricityCost (Reading initialReading finalReading) rate days daysPerMonth =
let usage = finalReading - initialReading
usagePerDay = usage / days
in usagePerDay * daysPerMonth * rate
gasCost :: Reading -> Double -> Double -> Double -> Double
gasCost (Reading initialReading finalReading) rate days daysPerMonth =
let usage = finalReading - initialReading
usagePerDay = usage / days
in usagePerDay * daysPerMonth * rate
waterCost :: Reading -> Double -> WaterExtraCost -> Double -> Double -> Double
waterCost (Reading initialReading finalReading) rate (WaterExtraCost waterborneRate taxRate sanitaryFee) days daysPerMonth =
let usage = finalReading - initialReading
usagePerDay = usage / days
waterFee = usagePerDay * daysPerMonth * rate
waterborneFee = usagePerDay * daysPerMonth * waterborneRate
tax = waterFee * taxRate
in waterFee + waterborneFee + sanitaryFee + tax
utilitiesCost :: ReadingRange -> ReadingRange -> ReadingRange -> Double -> Double -> Double
utilitiesCost (ReadingRange electricityReading) (ReadingRange gasReading) (ReadingRange waterReading) days daysPerMonth =
traceShow (electricityFee, gasFee, waterFee, refuseFee, gst) $ totalFee + gst
where
totalFee = electricityFee + gasFee + waterFee + refuseFee
refuseFee = 29
gstRate = 0.07
gst = gstRate * totalFee
electricityFee = electricityCost (Reading initialReading finalReading) rate days daysPerMonth
where initialReading = fst electricityReading
finalReading = snd electricityReading
rate = 0.2608
gasFee = gasCost (Reading initialReading finalReading) rate days daysPerMonth
where initialReading = fst gasReading
finalReading = snd gasReading
rate = 0.2118
waterFee = waterCost (Reading initialReading finalReading) rate (WaterExtraCost waterborneRate taxRate sanitaryFee) days daysPerMonth
where initialReading = fst waterReading
finalReading = snd waterReading
(rate, waterborneRate, taxRate) = (1.17, 0.5607, 0.3)
sanitaryFee =
let months = ceiling (daysPerMonth / 31.0)
in 2.28037 * fromIntegral months
@sovanlandy2
Copy link

nerds house billing is full of complexity that they decide to approach it with their programming skill.

@jessearmand
Copy link
Author

Yes, @sovanlandy2 we need to maximize our time to avoid complexity. So little time, so much to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment