0
answerFalse::Int->IO()
answerFalse hp=do
            hp--
            if hp<=0 then
                putStrLn"================Game Over================"

            else
                print(hp)

i already declare hp as int with value 3 now my problem is when i put "hp--", it shows error

 Couldn't match expected type `IO a0' with actual type `Int'

but if i put "--hp", the result print is 3 not 2.

i also tried let hp=hp-1, the system stuck there.

user1151874
  • 269
  • 3
  • 5
  • 15

2 Answers2

6

You can't modify variables in Haskell. hp++ and ++hp (or hp-- and --hp) don't work in Haskell at all; the reason why --hp compiles is that -- creates a comment which comments out the hp part.

What you are trying to do is this:

answerFalse hp =
  if hp - 1 <= 0
  then putStrLn "================Game Over================"
  else print hp

You can also do it like this, by creating a new variable:

answerFalse hp = do
  let newHp = hp - 1
  if newHp <= 0
    then putStrLn "================Game Over================"
    else print hp

You need to review your basic Haskell knowledge by reading beginner tutorials. This question provides excellent resources for you.

Community
  • 1
  • 1
dflemstr
  • 25,947
  • 5
  • 70
  • 105
  • thanks for the link and explaination, this is my first time to learn functional language. Thanks again! – user1151874 Feb 11 '12 at 16:25
  • and `hp--` introduces a comment *after* `hp`. In the `do` context, `hp` is expected to have type `IO a0` where `a0` is some arbitrary type which does not matter, since the value is not bound. However, `hp` has type `Int`, which leads to the error about not being able to match `IO a` with `Int`. – pat Feb 12 '12 at 09:19
0

things like i++ can be modelled in Haskell by using State monad. You can look examples on haskellwiki

Rijk
  • 612
  • 1
  • 7
  • 14