0
votes

how do i solve this problem in haskell?

" A machine selects an integral number ( more than or equal to one and less than or equal to N) with equal probability at one operation. Find the excepted probability in which the median of three integral number selected by three times operation with the machine above is K. (K is also integral number) "

first i tried this and got this error:

main = do

    n <- getLine
    k <- getLine

    print 1+(n-1)+(n-1)*(n-2)/n^3

q4.hs:7:18: Couldn't match type ‘[Char]’ with ‘IO ()’ Expected type: IO () Actual type: String In the first argument of ‘(-)’, namely ‘n’ In the second argument of ‘(+)’, namely ‘(n - 1)’

q4.hs:7:24: Couldn't match type ‘[Char]’ with ‘IO ()’ Expected type: IO () Actual type: String In the first argument of ‘(-)’, namely ‘n’ In the first argument of ‘(*)’, namely ‘(n - 1)’

q4.hs:7:30: Couldn't match type ‘[Char]’ with ‘IO ()’ Expected type: IO () Actual type: String In the first argument of ‘(-)’, namely ‘n’ In the second argument of ‘(*)’, namely ‘(n - 2)’

q4.hs:7:35: Couldn't match type ‘[Char]’ with ‘IO ()’ Expected type: IO () Actual type: String In the first argument of ‘(^)’, namely ‘n’ In the second argument of ‘(/)’, namely ‘n ^ 3’

how do i solve this problem? sorry i'm very new to haskell(and english too.) thank you.

1

1 Answers

5
votes

The first level of problems can be solved by adding parentheses to group the argument to print (or by using $):

main = do
    n <- getLine
    k <- getLine
    print (1+(n-1)+(n-1)*(n-2)/n^3)
    -- OR
    print $ 1+(n-1)+(n-1)*(n-2)/n^3

The next level of problems is that n is a String, but you're hoping for it to be some kind of number. You can fix this by using readLn instead of getLine and specifying what type you want to use; for example:

main = do
    n <- readLn :: IO Double
    k <- getLine
    print $ 1+(n-1)+(n-1)*(n-2)/n^3