简体   繁体   中英

Divide Int to Int and return Int

I need a function which gets two Int s ( 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 ?

Why not just use quot ?

quot a b

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

Here's what I did to make my own:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM