this code:
main :: io() main = { 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") { code 1 etc main } else if(opt == "2") { code 2 etc main } else if(opt == "3") { code 3 etc main } else if(opt == "4") { code 4 etc main } else if(opt == "5") { ??????????? () } else putstrln "option not exist"; }
the problem: in option 5 (opt == 5)
need make code stop menu, dont know how can this. tried find more examples on google , stackoverflow, can't find solution.
return ()
work here. in case, return
behaves in procedural language (but watch out, not true).
note on style: chains of if
...else
equality comparisons un-idiomatic in haskell. right way case
:
main = putstrln "meniu: " sequence_ [ putstrln $ [n]++". menu "++[n] | n<-['1'..'5'] ] putstrln "-------------------------" putstr "enter option: " opt <- getline case opt of "1" -> code 1 etc main "2" -> code 2 etc main "3" -> code 3 etc main "4" -> code 4 etc main "5" -> return () _ -> putstrln "option not exist"
braces , semicolons aren't needed if correctly indent code.
what return ()
here simply... nothing @ all, it's no-op. because main
ends after case
switch, program end if don't recurse main
in other options.
Comments
Post a Comment