【发布时间】:2017-10-04 19:00:22
【问题描述】:
我正在制作一个简单的 Haskell 待办事项列表,我想递归调用一个函数“提示”并根据用户输入显示不同的菜单选项。问题是,在初始调用时,prompt 应该期望接收一个函数作为不期望任何参数本身的参数之一。 “提示”的任何后续调用都可能需要调用一个函数,该函数确实期望将参数传递给正在传递给提示的函数。
这是我的代码:
mainMenuOptions :: IO ()
mainMenuOptions = do
putStrLn ""
putStrLn "What would you like to do?"
putStrLn ""
putStrLn "OPTIONS"
putStrLn ""
putStrLn "'+' : add items | '-' : remove items"
subMenuOptions :: [String] -> String -> IO ()
subMenuOptions todos operation = do
putStrLn ""
if operation == "add"
then do putStrLn ("Type in the TASK you'd like to " ++ operation ++ ", then hit ENTER")
addListItemOptions todos
else do
putStrLn ("Type in the NUMBER of the task you'd like to " ++ operation ++ ", then hit ENTER")
putStrLn "('r' : return to the main menu)"
prompt :: [String] -> IO () -> IO ()
prompt todos showOptions = do
showTasks todos
showOptions
input <- getLine
interpret input todos
interpret :: String -> [String] -> IO ()
interpret input todos
| input == "r" = prompt todos mainMenuOptions
| input == "+" = prompt todos subMenuOptions "add"
| input == "-" = prompt todos subMenuOptions "remove"
| otherwise = do
putStrLn ""
putStrLn "SORRY, did not get that! Please enter a valid option."
prompt todos mainMenuOptions
main :: IO ()
main = do
putStrLn "Haskell ToDo List"
prompt [] mainMenuOptions
我尝试这样做时遇到的错误是:
无法匹配预期类型“[Char] -> IO ()” 实际类型为“IO ()” • 函数“prompt”应用于三个参数, 但是它的类型‘[String] -> IO() -> IO()’只有两个
【问题讨论】:
标签: haskell