我想让程序跳回选项部分,不要为变量“expression”赋值
我试着制作像嵌套的do函数:
main = do
...
option = do
然后我会在某些情况下只调用“选项”,但它似乎没有这样工作,我找不到任何地方任何不同的解决方案。请问有人给出建议吗?
main = do
putStrLn "Enter expression:"
expression <- getLine
putStrLn "Enter a number from 1 to 5:"
putStrLn "1. Convert to Postfix"
putStrLn "2. Convert to Prefix"
putStrLn "3. Convert to Binary Expression Tree"
putStrLn "4. Substitution"
putStrLn "5. Evaluate Expression"
choice <- getLine
case (choice) of
"1" -> putStrLn $ toPostfix expression
"2" -> putStrLn $ toPrefix expression
"3" -> print (parse expression)
"4" -> do { putStrLn "Enter a variable: "
; variable <- getLine
; putStrLn "Enter a number: "
; number <- getLine
; print (sub expression variable number)
; print (solvePostfix (toPostfix (sub expression variable number))) }
"5" -> print (solvePostfix (toPostfix expression))
otherwise -> putStrLn "This is not a number from 1 to 5!"
答案 0 :(得分:5)
我不完全理解这个问题,但也许你正在寻找类似的东西。
main = do
putStrLn "Enter expression:"
expression <- getLine
putStrLn "Enter a number from 1 to 5:"
putStrLn "1. Convert to Postfix"
putStrLn "2. Convert to Prefix"
putStrLn "3. Convert to Binary Expression Tree"
putStrLn "4. Substitution"
putStrLn "5. Evaluate Expression"
choice <- getLine
let option = do
{ putStrLn "Enter a variable: "
; variable <- getLine
; putStrLn "Enter a number: "
; number <- getLine
; print (sub expression variable number)
; print (solvePostfix (toPostfix (sub expression variable number))) }
case (choice) of
"1" -> putStrLn $ toPostfix expression
"2" -> putStrLn $ toPrefix expression
"3" -> print (parse expression)
"4" -> option
"5" -> print (solvePostfix (toPostfix expression))
otherwise -> putStrLn "This is not a number from 1 to 5!"
或者,如果你想要一个循环:
main = do
putStrLn "Enter expression:"
expression <- getLine
let loop = do
putStrLn "Enter a number from 1 to 5:"
putStrLn "1. Convert to Postfix"
putStrLn "2. Convert to Prefix"
putStrLn "3. Convert to Binary Expression Tree"
putStrLn "4. Substitution"
putStrLn "5. Evaluate Expression"
choice <- getLine
case (choice) of
"1" -> putStrLn $ toPostfix expression
"2" -> putStrLn $ toPrefix expression
"3" -> print (parse expression)
"4" -> do
{ putStrLn "Enter a variable: "
; variable <- getLine
; putStrLn "Enter a number: "
; number <- getLine
; print (sub expression variable number)
; print (solvePostfix (toPostfix (sub expression variable number))) }
"5" -> print (solvePostfix (toPostfix expression))
otherwise -> putStrLn "This is not a number from 1 to 5!"
loop -- restart loop from start
-- in main, start loop
loop
可以使用库中的forever
帮助程序实现此循环,但了解基本的递归解决方案非常重要。