[Haskell-beginners] Perfect numbers

Matthew Williams Matthew_Williams at xyratex.com
Thu Oct 2 00:45:01 EDT 2008

Hi Guys,
I'm new to Haskell and I was wondering if you can help me:
One of the first program's I tend to write when I'm looking at a new
language is a program to generate a list of perfect numbers:
--My First Perfect Number Generator
factors :: Integer -> [Integer]
factors x = [z | z <- [1..x-1], x `mod` z == 0]
is_perfect :: Integer -> Bool
is_perfect x = if sum(factors x) == x then True else False
do_perfect :: [Integer] -> [Integer]
do_perfect x = [z |z <- x, is_perfect z ]
Then to run it:
> do_perfect [1..9000]
I'm using GHC to run it. My problem / question is this: It's running
quite a lot slower than equivalent programs in erlang and python. I
suspect it's down to the way I've written it. Any thoughts (or comments
in general)
Many thanks

This email may contain privileged or confidential information, which should only be used for the purpose for which it was sent by Xyratex. No further rights or licenses are granted to use such information. If you are not the intended recipient of this message, please notify the sender by return and delete it. You may not use, copy, disclose or rely on the information contained in it.

Internet email is susceptible to data corruption, interception and unauthorised amendment for which Xyratex does not accept liability. While we have taken reasonable precautions to ensure that this email is free of viruses, Xyratex does not accept liability for the presence of any computer viruses in this email, nor for any losses caused as a result of viruses.

Xyratex Technology Limited (03134912), Registered in England & Wales, Registered Office, Langstone Road, Havant, Hampshire, PO9 1SA.

The Xyratex group of companies also includes, Xyratex Ltd, registered in Bermuda, Xyratex International Inc, registered in California, Xyratex (Malaysia) Sdn Bhd registered in Malaysia, Xyratex Technology (Wuxi) Co Ltd registered in The People's Republic of China and Xyratex Japan Limited registered in Japan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081002/fb1929bb/attachment.htm

More information about the Beginners mailing list