English 中文(简体)
帮助解决使用警卫人员的问题 - 总统府
原标题:Help In Solving Problem Using Guards - UPDATED With CODE
  • 时间:2010-12-09 03:28:29
  •  标签:
  • haskell

我是在职能范围内使用警卫,但不是在职能签署后立即使用。 警卫人员在职能范围内发言do。 我发现这一错误:

parse error on input `| 

我认为,错误可能来自登革热,但我曾尝试过许多tation,但还是发现错误。 问,这是否是因为警卫在职能签字之后不是偶然的,这就是为什么出现错误?

感谢

<>12>

CODE: The user is suppose to guess a number , and the number will be compared with the random number if they are the same. If it is not correct then the user will guess till the "guess" variable in the function is zero. in every interation that value(guess) is decreased by one.

例如:puzz 12 5 。 用户可以进行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)

put:

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.

感谢 for your help

最佳回答

应当指出的是,除了使用警卫作错外,还有几件事情与你的法典脱节。 如果你希望Guess与你不得不表示的 st脚石一样,否则就会在仓促中缓冲未兑现的产出。 不应对外包进行缓冲(hSetBuffering endOut NoBuffering),或你必须用冲动产出。 页: 1 没有必要写上<代码> 在t 上,汇编者知道该编码是印的。 这里是一个更为完整的例子,我确信,可以做得更好,但至少可以做到:

import Control.Monad(liftM,unless)
import System.IO(hFlush,stdout)
import System.Random(randomRIO)

guessMyNumber upper guesses = do 
  putStrLn $ "Guess a number between 1 and " ++ show upper ++ "!"
  randomRIO (1, upper) >>= puzz guesses

puzz 0 number = do
  putStrLn $ "Sorry, no more guesses, the number was " 
          ++ show number ++ "."

puzz guesses number = do  
  putStr "Guess:" >> hFlush stdout 
  guess <- liftM read getLine
  printMessage guess number guesses

printMessage guess number guesses
  | number > guess = putStrLn "higer" >> puzz (guesses-1) number  
  | number < guess = putStrLn "lower" >> puzz (guesses-1) number
  | number == guess =  putStrLn "Congratulations! You guessed right!"
问题回答

页: 1 摘自报告:

案件表达方式的顶级模式以及具有职能或约束模式的顶级模式可能会有零或更多的相关警卫。

因此,它们只针对案件和职能具有约束力。 如果你只是想简明地介绍一系列真正的数字测试,同时在说明中,或许(case()的“. trick”将发挥作用:

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




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签