Good Haskell coding style of if/else control block?

后端 未结 8 1551
慢半拍i
慢半拍i 2021-01-31 09:15

I\'m learning Haskell in the hope that it will help me get closer to functional programming. Previously, I\'ve mostly used languages with C-like syntax, like C, Java, and D.

8条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 09:53

    You can use the "case"-construct:

    doGuessing num = do
        putStrLn "Enter your guess:"
        guess <- getLine
        case (read guess) of
            g | g < num -> do 
                putStrLn "Too low!"
                doGuessing num
            g | g > num -> do 
                putStrLn "Too high!"
                doGuessing num
            otherwise -> do 
                putStrLn "You Win!"
    

提交回复
热议问题