I just started learning Haskell. I decided to set myself a goal of implementing an old algorithm of mine http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.79.7006&rep=rep1&type=pdf
As a start 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 x y) of LT -> x : minus xs (y:ys) EQ -> minus xs ys GT -> minus (x:xs) ys minus xs _ = xs This functions as expected, except that the list of primes comes as floating point! A little thought told me that since the signature of sqrt is
sqrt :: (Floating a) => a -> a the Haskell compiler has decided that primes is returning a list of floats. However, when I tried to tell it that
phi :: [Integer] -> [Integer] which is what I want, the compiler has a problem:
No instance for (Floating Integer) arising from a use of `sqrt` at ... So how do I signify the phi takes as input a list of integers and as output produces an infinite list of Integers?
sqrtwith other functions, likefromIntegralandfloor, to get a square root function of the right type.(tail phi $ reverse smallprimes)doesn't seem to make sense, nor does theLTcase inminus.$betweentailandphithis is indeed a strange thing. Even if the argumet toprimesis floating point, there is no way this argument carries over tophi. So I have doubts the code is really the one you compiled?