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.
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
Related
let f = show.sum.map read.words
f "1 2"
It work.
show.sum.map read.words "1 2"
I get errors:
<interactive>:19:19:
Couldn't match expected type ‘a -> [String]’
with actual type ‘[String]’
Relevant bindings include
it :: a -> String (bound at <interactive>:19:1)
Possible cause: ‘words’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘words "1 2"’
In the second argument of ‘(.)’, namely ‘map read . words "1 2"’
Prelude> :t show.sum.map
show.sum.map
:: (Num [b], Show b, Foldable ((->) [a])) => (a -> b) -> String
Prelude> show.sum.map read.words "1 2"
<interactive>:21:19:
Couldn't match expected type ‘a -> [String]’
with actual type ‘[String]’
Relevant bindings include
it :: a -> String (bound at <interactive>:21:1)
Possible cause: ‘words’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘words "1 2"’
In the second argument of ‘(.)’, namely ‘map read . words "1 2"’
Prelude>
I want to know why? (show.sum.map read.words) "1 2" can also work.
The issue is that application binds more tightly than application which in fact has the highest precedence of any Haskell operator. This means that your code actually parses
show.sum.map read.(words "1 2")
which doesn't type check because words "1 2" has the type String, not something that can be composed with .. In order to fix this you can use the $ operator which is specifically designed to behave as application but which the lowest precedence instead of the highest.
show.sum.map read.words $ "1 2"
will work as intended because it has the correct association. This is in general why you'll see the pattern in Haskell code (well at least my Haskell code) of foo . bar . baz $ quux quite frequently.
Here is the problem. It looks simple yet
main = do
s <- getContents
let list = map (read::Int) (words s)
print list
Couldn't match expected type `Int' with actual type `String -> a0'
Probable cause: `read' is applied to too few arguments
In the first argument of `map', namely `(read :: Int)'
In the expression: map (read :: Int) (words s)
The problem was that I thought :: is like casting and I have to put the return type. The solution was to add full wanted |function signature instread.
read is a function (of type Read a => String -> a), so it can't have type Int. You could do read :: String -> Int, or you could put a type signature on list rather than read, so you get:
let list :: [Int]
list = map read (words s)
Can someone please explain what's going on in my function.
arrayReader :: [Int] -> IO [Int]
arrayReader arr = do
item <- readLn
return $ if item == 0
then arr
else arrayReader item:arr
But Haskell is not happy with the 6th line:
reader.hs:6:17:
Couldn't match expected type `Int' with actual type `IO [Int]'
In the return type of a call of `arrayReader'
In the first argument of `(:)', namely `arrayReader item'
In the expression: arrayReader item : arr
Can someone explain what needs to be changed to make this function compile?
Firstly, you have a precedence error - arrayReader item:arr parses as (arrayReader item):arr. You need to write arrayReader (item:arr).
Secondly, arrayReader produces something of type IO [Int], but in this context return takes something of type [Int] and produces IO [Int]. You need to rearrange your code so that return is only called on arr, not on the result of arrayReader.
I've got this simple function:
bombplaces::Int->[(Int,Int)]->[(Int,Int)]
bombplaces bombCount listOfPossiblePoints = nub (map (take bombCount) (perms listOfPossiblePoints))
bombs are (x,y) (carthesian points)
i need to get an all permutations and take only first few (bombCount) points.
I'm getting following error:
Couldn't match expected type `(Int,Int)' with actual type `[a0]'
Expected type: [a0] -> (Int,Int)
Actual type: [a0] -> [a0]
In the return type of a call of `take'
In the first argument of `map', namely `(take liczbaBomb)'
If you remove the type signature and ask GHCi for the type, your problem will be obvious:
> :t bombplaces
bombplaces :: Eq a => Int -> [a] -> [[a]]
That is, bombplaces wants to return a list of lists whereas you want it to return a plain list. You need to either change the type signature, or change the definition of the function, depending on what you want the behaviour to be.
N.B. You didn't tell us what definition of perms you are using, so I assumed the obvious one.
I do:
Prelude> "sone" ++ "otehr"
"soneotehr"
But such code:
addOneToElement :: [a] -> [a]
addOneToElement element = element ++ "next"
main = do
let s = addOneToElement("some")
putStrLn s
produces this output:
all_possible_combinations.hs:22:37:
Couldn't match expected type `a' against inferred type `Char'
`a' is a rigid type variable bound by
the type signature for `addOneToElement'
at all_possible_combinations.hs:21:20
Expected type: [a]
Inferred type: [Char]
In the second argument of `(++)', namely `"next"'
In the expression: element ++ "next"
Why I get this error and how I can fix it?
Your type signature should be:
addOneToElement :: [Char] -> [Char]
(Or more simply, addOneToElement :: String -> String)
The "a" in your type signature is a wildcard - it can match anything. However you are trying to concatenate a list of Char to a list of anything - and there is no way to do that.
Why are you using a type variable here anyway? The only type that can match is Char, since the second operand of (++) is fixed to [Char] ("next").