KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I just started Haskell and it completely confuses me. I did Java and Python before which made a lot more sense to me. I'm currently trying to get a user input, check if it is valid, if not: print an error and get the input again; if valid: produce a a boolean value from it. To be more precise, I want a yes/no input, where 'y' will produce True, 'n' will produce False, and any other input will print a message into the command line and ask to input y/n again. E.g.: Continue? y/n: > assd Invalid input. Continue? y/n: > y (something happens) Continue? y/n: > n (Close program) Writing it in farmiliar format, a function like this: boolean inputBool() { while(True) { str = input("Continue? y/n: "); if (str == "y") { return True; } else if (str == "n") { return False; } else { print("Invalid input"); } } --main program-- while(inputBool()) { doSomething(); } Since I just started haskell (today actually), I don't have much of an idea what I can or can't do. I was thinking of something similar to: yesno :: Bool yesno = do putStr "Continue? y/n: " str <- readLn if (str == "y") then True else ( if (str == "n") then False else ( do putStrLn "Invalid input." yesno ) ) Which doesn't work for many reasons. My main problem is that I don't know what this "do" does. I just read it's used when needing io operations and somehow executes the following expressions. Which doesn't make sense to me after reading that in haskell everything evaluates to a value. What does "do" evaluate to? Also, what indentation is expected? It seems to be kind of random. I know that the function has to evaluate to my boolean value, which doesn't seem to be possible with using this "do" o
Tags (comma-separated)
Save Edits
Cancel