[Haskell-beginners] Prime number test performance on negative integers
Francesco Ariis
fa-ml at ariis.it
Mon Dec 11 17:55:21 UTC 2023
Hello Pratima,
Il 11 dicembre 2023 alle 19:24 Folsk Pratima ha scritto:
> Please explain why this fails for negative numbers. By fails I mean it
> starts eating RAM infinitely until either me or OOM kills it. If you
> replace `m1` definition with the one commented out, the code will fail
> for positive integers as well, which is very frustrating.
I have replaced m1 definition with the commented one, and it works on
my machine.
f at x270:/tmp/prova$ ./prime -4
-4 is not primal because divisible by 2
How are you invoking the program?
A few additional notes (run `hlint` for more)
> main =
> getArgs >>= \argv ->
I don’t mind `>>=` but with `do` notation and `traceM/traceShowM` it
is easier to debug your programs.
> --m1 =
> -- map
> -- (\x ->
> -- if x < 0
> -- then -x
> -- else x)
> -- m0
More compact: `m1 = map abs m0`
> msg (x, (y0, y1)) = x ++ " is " ++ f y0 ++ " because " ++ y1
Ancillary functions like this one can go in the `where` section.
Ciao
—F
More information about the Beginners
mailing list