How to correctly determine the Haskell isPrime function?

I am trying to create a basic function for checking the primitiveness of an integer in Haskell. I have code that will work in a special sense, but keep getting an error message when I try to pass it to a function. Note that I write the definitions directly in GHCi using :{and :}.

The idea is to create a list from N modulo {all integers to rounded sqrt (N)}, and then check the resulting list for zero, except for the starting index. The following four functions work:

rndRoot :: (Floating a, Integral c, RealFrac a) => a -> c
rndRoot = round . sqrt

oneToRndRoot :: (Floating a, Integral t, RealFrac a) => a -> [t]
oneToRndRoot x = [1..rndRoot(x)]

modulo x y
  | n < 0 = x
  | otherwise = modulo n y
  where n = x - y

mapMod x = map (modulo x)

This also works:

mapMod 49 (oneToRndRoot 49)

However, although repl accepts this definition without complaint ...

mapModToRndRoot x = mapMod x $ oneToRndRoot x

... it pops up the following error message when I try to use it:

Prelude> mapModToRndRoot 39

<interactive>:475:1:
    No instance for (Floating a0) arising from a use of ‘it’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Floating Double -- Defined in ‘GHC.Float’
      instance Floating Float -- Defined in ‘GHC.Float’
    In the first argument ofprint’, namely ‘it’
    In a stmt of an interactive GHCi command: print it

ad hoc, , , , - ,

mapModToRndRoot2 x y = map (modulo x) (oneToRndRoot y)
Prelude> mapModToRndRoot2 33 33
[0,1,0,1,3,3]
+4
1

mapModToRndRoot :

mapModToRndRoot :: (Floating a, Integral a, RealFrac a) => a -> [a]

, mapModToRndRoot 39, 39, RealFrac, Integral Floating. Prelude , .

, mapMod 49 (oneToRndRoot 49) . :

\x y -> mapMod x (oneToRndRoot y)
  :: (RealFrac a, Integral b, Floating a) => b -> a -> [b]

, GHC , .

: , @duplode:

rndRoot :: (Integral a) => a -> a
rndRoot = round . sqrt . fromIntegral

fromIntegral . round . sqrt , .

: @WarrickMacmillan, oneToRndRoot. .

rndRoot :: Integral a => a -> a
rndRoot = round . sqrt . fromIntegral

oneToRndRoot :: Integral a => a -> [a]
oneToRndRoot x = [1..rndRoot(x)]

modulo :: (Ord p, Num p) => p -> p -> p
modulo x y
  | n < 0 = x
  | otherwise = modulo n y
  where n = x - y

mapMod :: (Num b, Ord b) => b -> [b] -> [b]
mapMod x = map (modulo x)

mapModToRndRoot :: Integral a => a -> [a]
mapModToRndRoot n = mapMod n (oneToRndRoot n)
+5

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


All Articles