帮助解决使用警卫的问题 - 使用CODE更新

kap*_*kap 1 haskell

我在函数内部使用了警卫但在函数签名后没有立即使用.守卫在函数体内的do语句下.我收到此错误:

parse error on input `|'
Run Code Online (Sandbox Code Playgroud)

我想也许错误来自缩进,但我尝试了很多缩进,但我仍然得到错误.我问的是因为在功能签名之后卫兵不是立即得到错误的原因吗?

谢谢

更新1

代码:用户猜测一个数字,如果数字相同,则将数字与随机数进行比较.如果不正确,则用户将猜测直到函数中的"guess"变量为零.在每个阶段,价值(猜测)减少一个.

例如:puzz 12 5.用户可以猜测五次,随机数将在1到12之间被选中.这就是函数的假设,但它不起作用.

puzz :: Int -> Int -> IO ()
puzz boundary guess = do
                          putStr "Guess" 
                          -- putStr  -- I have to print (1 .. guess) here in each iteration
                          putStr ":"
                          x <- randomRIO (1, boundary :: Int)
                          n <- getLine
                          let
                             nTo = read n::Int
                            in print x
                               | guess == 0 = putStr "You couldn't guess right,the   correct answer is" ++ x
                               | nTo > x = putStr "lower"
                               | nTo < x = putStr "higer"
                               | nTo == x =  putStr "Congrat, You guess right."
                               | otherwise raad boundary (guess - 1)
Run Code Online (Sandbox Code Playgroud)

输出必须是这样的:

Main> puzz 50 6
Guess a number betwee 1 en 50.
Guess 1: 49
lower
Guess 2: 25
lower
Guess 3: 12
higher
Guess 4: 18
higher
Guess 5: 21
higher
Guess 6: 23
lower
You couldn't guess correct, the answer was: 22.
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Jos*_*Lee 5

你错误地使用了警卫.从报告中:

case表达式中的顶级模式和函数或模式绑定中的顶级模式集可以具有零个或多个关联的防护.

因此它们仅用于案例和功能绑定.如果你只是想简明扼要地介绍一系列真假测试,那么在一个注释中,也许这个case () of ()技巧会起作用:

main = do
    putStrLn "hello world"
    n <- getLine
    let nTo = read n :: Int
    case ()
      of () | cond -> putStrLn "foo"
            | cond' -> putStrLn "bar"
            | otherwise -> putStrLn "baz"
Run Code Online (Sandbox Code Playgroud)