Divide Int to Int and return Int

HaskellInt

Haskell Problem Overview


I need a function which gets two Ints (a and b) and returns A/B as Int. I am sure that A/B will always be an integer.

Here is my solution:

myDiv :: Int -> Int -> Int
myDiv a b = 
      let x = fromIntegral a
          y = fromIntegral b
      in truncate (x / y)

But want to find more simpler solution. Something like this:

myDiv :: Int -> Int -> Int
myDiv a b = a / b

How can I divide Int to Int and get Int ?

Haskell Solutions


Solution 1 - Haskell

Why not just use quot?

quot a b

is the integer quotient of integers a and b truncated towards zero.

Solution 2 - Haskell

Here's what I did to make my own:

quot' a b
         | a<b = 0  -- base case
         | otherwise = 1 + quot' a-b b

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncethView Question on Stackoverflow
Solution 1 - HaskellPointyView Answer on Stackoverflow
Solution 2 - HaskellJacob StewartView Answer on Stackoverflow