I'm trying to create a basic function to test primality of an integer in Haskell. I have code which will work in an ad hoc sense, but continue to get an error message when I try to pass it to a function. Note that I'm writing the definitions directly in GHCi, using :{
and :}
.
The idea is to create a list of N modulo {all integers up to a rounded sqrt (N)}, and then test the resulting list for a zero other than the initial index. The following four functions all 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, while the repl accepts this definition without complaint...
mapModToRndRoot x = mapMod x $ oneToRndRoot x
... it spits out 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 of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
The ad hoc solution which seems to work fine is just to use two arguments rather than repeat the same one
mapModToRndRoot2 x y = map (modulo x) (oneToRndRoot y)
Prelude> mapModToRndRoot2 33 33
[0,1,0,1,3,3]