I have created an algorithm which takes a non-negative Int value, representing a total number of minutes, and returns a triple that gives (days, hours, minutes) that this corresponds to.
Here is my code:
calcdays :: Int -> Int
calcdays x = x `div` (24*60)
calchours :: Int -> Int
calchours x = (x - ((calcdays x)*24*60)) `div` 60
calcmins :: Int -> Int
calcmins x = (x - ((calcdays x)*24*60) - ((calchours x)*60))
dayshoursmins :: Int -> (Int,Int,Int)
dayshoursmins x = (calcdays x, calchours x, calcmins x)
Using only basic Haskell operations (guards, divs, mods etc.), is there a more simple way of programming this function?
EDIT:
I have used a suggestion below to make this code simpler, while not as simple as the qoutRem solution, I thought I might post it:
calcdays :: Int -> Int
calcdays x = x `div` (24*60)
calchours :: Int -> Int
calchours x = (x `mod` (24*60)) `div` 60
calcmins :: Int -> Int
calcmins x = (x `mod` (24*60)) `mod` 60
dayshoursmins :: Int -> (Int,Int,Int)
dayshoursmins x = (calcdays x, calchours x, calcmins x)
take the input, divide it by 60, the remainder is the minute part, the result is then divided by 24, the remainder is the hours part, the result is the days part. So you'd need to chain the calculations rather than execute them in isolation.