Reputation: 145
This is the code:
main :: IO()
main = do
{
putStrLn ("Meniu: ");
putStrLn ("1. menu 1");
putStrLn ("2. menu 2");
putStrLn ("3. menu 3");
putStrLn ("4. menu 4");
putStrLn ("5. Exit - Iesire)");
putStrLn ("-------------------------");
putStr ("Enter option: ");
opt <- getLine;
if(opt == "1") then do
{
Code one etc
main
}
else if(opt == "2") then do
{
Code 2 etc
main
}
else if(opt == "3") then do
{
code 3 etc
main
}
else if(opt == "4") then do
{
code 4 etc
main
}
else if(opt == "5") then do
{
??????????? ()
}
else putStrLn "Option is not exist";
}
The Problem:
In the option 5 (opt == 5)
I need to make an code to stop the menu, but I dont know how I can do this.
I tried to find more examples on Google and StackOverflow, but I really can't find a solution.
Upvotes: 1
Views: 678
Reputation: 120711
return ()
will work here. In this case, return
actually behaves like it would in a procedural language (but watch out, this is not generally true).
Note on style: chains of if
...else
with equality comparisons are very un-idiomatic in Haskell. The right way to do this is a case
:
main = do
putStrLn "Meniu: "
sequence_ [ putStrLn $ [n]++". menu "++[n] | n<-['1'..'5'] ]
putStrLn "-------------------------"
putStr "Enter option: "
opt <- getLine
case opt of
"1" -> do
Code one etc
main
"2" -> do
Code 2 etc
main
"3" -> do
code 3 etc
main
"4" -> do
code 4 etc
main
"5" -> return ()
_ -> putStrLn "Option does not exist"
Braces and semicolons aren't needed if you correctly indent the code.
What return ()
does here is simply... nothing at all, it's the no-op. Because main
ends after the case
switch, the program will then also end if you don't recurse back to main
like in the other options.
Upvotes: 9