Допис з розмови
Newbie question...
Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!news2.arglkargh.de!news.visyn.net!open-news-network.org!visyn.net!not-for-mail
From: "Mark T.B. Carroll" <Mark.Carr...@Aetion.com>
Newsgroups: comp.lang.haskell
Subject: Re: Newbie question...
Date: Sat, 13 Jun 2009 08:28:15 -0400
Organization: Aetion Technologies LLC, Ohio, USA
Lines: 38
Message-ID: <871vponwrk.fsf@ixod.org>
References: <d6c02a34-b116-44fa-85e3-50bfcd1a2c44@y7g2000yqa.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: hoshi.visyn.net tc551SBfSCW3frtBFmnloodzOrJghwUj4u+NLaMdVCk=
X-Complaints-To: abuse@open-news-network.org
NNTP-Posting-Date: Sat, 13 Jun 2009 12:28:16 +0000 (UTC)
X-User-ID: VzPG/V+bEHRMDwNlwrm/UAhdkfg1boOLIhyaCPSQWlk=
Cancel-Lock: sha1:oBS8OX1O0GEwNJHVM+2iz0OQ+Fk=
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)
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