
I've got a little problem i don't understand. I need to calculate an index for a list (by use of !!). It should look like this: floor(j * (n / p2n)) where j is an Integer, n is the length of a list, therefore Integer too, and p2n is 2^n. When i use the former line in my Haskell code (pasted at the end of this mail), i get the following error message: qc.lhs:464:16: No instance for (RealFrac Int) arising from use of `floor' at qc.lhs:464:16-36 Possible fix: add an instance declaration for (RealFrac Int) In the expression: floor (j * (n / p2n)) In the definition of `i': i = floor (j * (n / p2n)) In the definition of `genUf'': genUf' f j n | j == p2n = [] | otherwise = [(replicate (j + (y1 - y0)) 0) ++ ([1] ++ (replicate (p2n - ((j + (y1 - y0)) + 1)) 0))] ++ (genUf' f (j + 1) n) where y0 = mod j 2 y1 = xor y0 (f !! i) xor a b = mod (a + b) 2 p2n = (2 ^ n) i = floor (j * (n / p2n)) Failed, modules loaded: none. ------------------------------------------------------------------------ genUf :: [Int] -> QOp genUf f = (QOp(genUf' f 0 (length f))) where p2n = 2 ^ (length f) {- ok -} genUf' f j n | j == p2n = [] {- ok -} | otherwise = [(replicate (j+(y1-y0)) 0) ++ [1] ++ (replicate (p2n-(j+(y1-y0)+1)) 0)] ++ (genUf' f (j+1) n) where y0 = mod j 2 y1 = xor y0 (f!!i) xor a b = mod (a+b) 2 p2n = (2 ^ n) i = floor (j * (n / p2n))

Hi Dennis, (/) :: Fractional a => a -> a -> a div :: Integral a => a -> a -> a Basically, use / on Float/Double like things, and div on Int/Integer like things: If you do want to use double like things throughout, then using fromInteger around the place will help: floor (fromInteger j * (fromInteger n / fromInteger p2n)) This converts easy value from an Integer to a number thing, which will allow you to treat it like a Double/Float thing. If you are using Int (rather than Integer) you might need fromInteger (toInteger x) around the place. Thanks Neil

On Jan 28, 2007, at 13:01 , Dennis Buchmann wrote:
It should look like this: floor(j * (n / p2n)) where j is an Integer, n is the length of a list, therefore Integer too, and p2n is 2^n.
When i use the former line in my Haskell code (pasted at the end of this mail), i get the following error message:
qc.lhs:464:16: No instance for (RealFrac Int) arising from use of `floor' at qc.lhs:464:16-36
It's telling you you're trying to take the floor of an Int (not a type in class RealFrac, which would be Float or Double). This makes sense because everything there is in fact an Int ((^) is defined as taking things of class Integral (Integer, Int) and returning something in class Num, which in this case will be Int because it's used in an Int expression). In other words, floor is unnecessary. -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (3)
-
Brandon S. Allbery KF8NH
-
Dennis Buchmann
-
Neil Mitchell