我对Haskell非常陌生,我正在尝试学习一些简单的函数。
我通过这样的函数做出了一些选择:
displayOptions :: Int -> String
displayOptions option
| option == 0 = "1 - Shop\n2 - Fight Monsters\n3 - Inn\n4 - Monk"
| otherwise = "invalid"然后使用getLine获取用户输入。
choice <- getLine然后显示第二个选项框,例如,
playerChoice :: String -> String
playerChoice option
| option == "1" = "Sword - 50gp\nShield - 100gp"
| option == "2" = "You go fight some monsters outside town."
| option == "3" = "You go to the town Inn."
| option == "4" = "You go see the holy monk."
| otherwise = "You entered invalid information...\n" ++ displayOptions(0)我感到困惑的是如何在函数中再次获得用户的选择。我希望我的otherwise =说出无效的信息,显示选项,再次获得输入,然后显示他们所做的选择。
所以我的主程序应该是这样的:
main = do
putStrLn "The king has sent you on the journey to become a master."
putStrLn $ "What would you like to do?"
putStrLn $ displayOptions(0)
choice <- getLine
putStrLn $ playerChoice(choice)发布于 2018-10-28 23:06:59
您必须将返回类型更改为IO字符串,而不是字符串。
但是,您可能希望返回任何一个字符串,以便该函数指示它返回了游戏进程文本Right "You do something"或一个失败,并解释了失败Left "Not an option"。
然后在调用者中循环,直到得到一个Right值,每次得到一个Left值,就会打印文本并再次询问。
我确信有一个稍微好一点的方法,但下面是一些快速修复代码:
module Main where
playerChoice :: String -> Either String String
playerChoice option
| option == "1" = Right "Sword - 50gp\nShield - 100gp"
| option == "2" = Right "You go fight some monsters outside town."
| option == "3" = Right "You go to the town Inn."
| option == "4" = Right "You go see the holy monk."
| otherwise = Left "You entered invalid information..."
displayOptions :: Int -> String
displayOptions option
| option == 0 = "1 - Shop\n2 - Fight Monsters\n3 - Inn\n4 - Monk\n"
| otherwise = "invalid"
main = do
let progress whathappens = do
putStrLn whathappens
let tryAsk prompt = do
putStrLn prompt
choice <- getLine
either tryAsk progress $ playerChoice(choice)
tryAsk $ displayOptions(0) ++ "What would you like to do?"
progress "The king has sent you on the journey to become a master."如果您是import Data.Function,那么您也可以像下面这样编写它--在本例中可能不是更好,但这是进入haskell迷人部分的一个不错的浅表步骤:
fix (\moreProgress whathappens -> do
putStrLn whathappens
fix (\askAgain prompt -> do
putStrLn prompt
choice <- getLine
either askAgain moreProgress $ playerChoice(choice))
$ displayOptions(0) ++ "What would you like to do?")
$ "The king has sent you on the journey to become a master."发布于 2018-10-28 23:10:29
Either String String,以便提供错误消息。有关更多细节,请查看Either类型的文档。for或while这样的传统循环结构,而是使用递归调用。有关示例,请参见While loop in Haskell with a condition。https://stackoverflow.com/questions/53036807
复制相似问题