Mod, div in Haskell

Can someone explain why this is not working?

main = do
    let a = 50
    let y = 7
    let area = (a ** y) 
    print (area)   
    print (a `mod` y)

I expected him to print:

781250000000   -- 50 to the 7th power
1              -- remainder of 50/7

But instead, I get a series of ambiguous errors like:

test.hs:2:13:
    No instance for (Num a0) arising from the literal `50'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
+4
source share
1 answer

Plain; take a look at the types (**)and mod:

Prelude> :t (**)
(**) :: Floating a => a -> a -> a
Prelude> :t mod
mod :: Integral a => a -> a -> a

This is a rare numeric type that has both integer characteristics and floating point characteristics. You have several options for solving this problem:

  • Use an exponential operation that handles integers well. Eg (^) :: (Integral b, Num a) => a -> b -> a.
  • Use a module operation that handles floating point numbers well. Eg mod' :: Real a => a -> a -> a.
  • realToFrac :: (Real a, Fractional b) => a -> b (**).
  • floor :: (RealFrac a, Integral b) => a -> b ( ) mod.
+8

Source: https://habr.com/ru/post/1619560/


All Articles