I just started to learn Haskell. I decided to set myself the goal of implementing my old algorithm http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.79.7006&rep=rep1&type=pdf
In the beginning I wrote the following code
phi [] = [1..] phi (p:pl) = (phi pl) `minus` (map (p*) $ phi pl) primes x | x < 2 = [] | otherwise = smallprimes ++ (takeWhile (<=x) $tail $ phi $ reverse smallprimes) where smallprimes = primes $ sqrt x minus (x:xs) (y:ys) = case (compare xy) of LT -> x : minus xs (y:ys) EQ -> minus xs ys GT -> minus (x:xs) ys minus xs _ = xs
This works as expected, except that the list of primes is a floating point! A little thought told me that since sqrt signature
sqrt :: (Floating a) => a -> a
the Haskell compiler decided that primes return a list of floats. However, when I tried to say that
phi :: [Integer] -> [Integer]
what i want, the compiler has a problem:
No instance for (Floating Integer) arising from a use of `sqrt` at ...
So, how can I indicate that phi accepts a list of integers as input, and since the output creates an infinite list of integers?
Victor Miller
source share