[Haskell-beginners] Need Help

kaushal Pathak kaushalkp at gmail.com
Sat Apr 11 08:16:47 EDT 2009

Hi There,
   Here is a very simple haskell program I have written to find whether a
number is prime or not, which doesn't give me any compilation error but
gives following run-time error :

Any help would help me move ahead, I know that the type concepts is what I
am lacking, anly pointer to simple/good article/paper would definitely help.

Lots of thannx jut even to look at the mail :-)


*Run Time Error :*

kaushal > isPrime1 171

    Ambiguous type variable `t' in the constraints:
      `Integral t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
      `Floating t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
      `RealFrac t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
    Probable fix: add a type signature that fixes these type variable(s)
kaushal >

*Program :*

isPrime1 x =
    let canDivide num 0 = 0
        canDivide num 1 = 0
        canDivide num divisor = if ((mod num divisor) == 0) then 1
                                else canDivide num (divisor - 1)
        if ( x == 1 )
        then putStrLn("1 is Neither prime nor composite!!!")
        else if ((canDivide x first_div) == 1)
             then putStrLn(show(x) ++ " is not a Prime Number!!!")
             else putStrLn(show(x) ++ " is a Prime Number!!!")
        first_div :: Integral a => a ; first_div = round (sqrt x)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090411/f855590c/attachment.htm

More information about the Beginners mailing list