
Hi, I am trying to write a simple function to determine the divisors of an integer. In its simplest form the type signature should be something like: divisors :: Int -> [Int] divisors x = 1 : lower ++ upper ++ x : [] where lower = filter (\y -> mod x y == 0) [2..(ceiling . sqrt) x] upper = sort $ map (div x) lower Although, I think my type signature isn’t complete as it ignores a lot of what’s going on in the function. Regardless, the function throws an error when evaluated. I don’t know if I just need a more accurate type signature, or if there is a bigger problem. From what I can tell, my problem is in the lower function, but when I deconstruct it into its parts, they work individually and as a whole, but not as a stand-alone function. For example, using x = 36: Prelude> [2..(ceiling . sqrt) 36] [2,3,4,5,6] Prelude> filter (\y -> mod 36 y == 0) it [2,3,4,6] Or as a whole: Prelude> filter (\y -> mod 36 y == 0) [2..(ceiling . sqrt) 36] [2,3,4,6] But when I define this as a function, it throws an error when evaluated. Prelude> let lower x = filter (\y -> mod x y == 0) [2..(ceiling . sqrt) x] Prelude> lower 36 <interactive>:6:1: No instance for (RealFrac b0) arising from a use of `lower' The type variable `b0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance RealFrac Double -- Defined in `GHC.Float' instance RealFrac Float -- Defined in `GHC.Float' instance Integral a => RealFrac (GHC.Real.Ratio a) -- Defined in `GHC.Real' In the expression: lower 36 In an equation for `it': it = lower 36 <interactive>:6:7: No instance for (Num b0) arising from the literal `36' The type variable `b0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Num Double -- Defined in `GHC.Float' instance Num Float -- Defined in `GHC.Float' instance Integral a => Num (GHC.Real.Ratio a) -- Defined in `GHC.Real' ...plus three others In the first argument of `lower', namely `36' In the expression: lower 36 In an equation for `it': it = lower 36 At this point, I’m not sure what I need to do to get his working properly. It appears to be a type error, but I’m apparently not understanding the error message enough to fix the problem. Any suggestions would be appreciated. Thanks, James