Haskell Converting Int to Float

前端 未结 1 626
情书的邮戳
情书的邮戳 2021-01-18 04:12

I\'m having some problem with one of the functions which I\'m new at, it\'s the fromIntegral function.

Basically I need to take in two Int arguments and return the p

相关标签:
1条回答
  • 2021-01-18 05:11

    Look at the type of div:

    div :: Integral a => a -> a -> a
    

    You cannot transform your input to a Float and then use div.

    Use (/) instead:

    (/) :: Fractional a => a -> a -> a
    

    The following code works:

    percent :: Int -> Int -> Float
    percent x y =   100 * ( a / b )
      where a = fromIntegral x :: Float
            b = fromIntegral y :: Float
    
    0 讨论(0)
提交回复
热议问题