Перейти до Домашньої сторінки Груп Google    comp.lang.haskell
Re: Newbie question...

Mark T.B. Carroll <mark.carr...@aetion.com>

maxino <max.korob...@gmail.com> writes:

(snip)

> proper_divisors :: Int -> [Int]
> proper_divisors n = [d | d <- [1 .. (floor . sqrt) n], mod n d == 0]
(snip)
>     No instance for (RealFrac Int)
>       arising from a use of `floor' at pd.hs:2:37-41
(snip)
>     No instance for (Floating Int)
>       arising from a use of `sqrt' at pd.hs:2:45-48

the (floor . sqrt) can't accept an n :: Int, because they need n's type
to be instances of Floating and RealFrac, and Int isn't.

Prelude> :type sqrt
sqrt :: (Floating a) => a -> a

... note that the return type is the same as the argument type. From a
commonsense point of view, one can see part of the reason for the
complaint about Int as being that for things like sqrt (2::Int) you
couldn't return an Int answer (at least a correct one!).

Of course,

Prelude> :type mod
mod :: (Integral a) => a -> a -> a

mod expects an integral n, and you already declared n to be Int.

How about,

proper_divisors n = [d | d <- [1 .. (floor . sqrt . fromIntegral) n], mod n d == 0]

By the way, though I actually much prefer your convention, upper and
lower camel case seem to have established themselves as the Haskell way,
so normally one would name your function properDivisors.

Mark