Haskell - More efficient way to complete an algorithm?

I created an algorithm that takes a non-negative Int value representing the total number of minutes, and returns a triple that gives (days, hours, minutes) that this matches.

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 the basic Haskell operations (guards, divs, mods, etc.), is there an easier way to program this function?

EDIT:

I used the sentence below to make this code simpler, although not as simple as the qoutRem solution, I thought I could 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) 
+6
source share
2 answers

I think something like this

 dayshoursmins x = (d,hr,mr) where (h,mr) = quotRem x 60 (d,hr) = quotRem h 24 
+12
source

you can use

 a `mod` b 

to directly receive the balance.

And you can use let or where to calculate the subexpression.

+3
source

All Articles