Haskell: whileM_ variable scope? - haskell

How can I use a variable scoped in the Bool section of whileM_ later in its body?
For example:
guess :: IO ()
guess = do
putStrLn "Please input your guess."
whileM_
(do
guess <- getLine
return (guess /= "secret"))
(do
putStrLn ("You guessed: " ++ guess)
putStrLn ("And " ++ guess ++ " is wrong."))
putStrLn "Right - Bye..."
guess is not in scope in the second do block so unavailable for use.
How can I bring it into scope?
Thank you.

whileM_ doesn't have a built-in way of achieving this. That combinator is really a bit limiting; as dfeuer says you should probably just write the loop yourself using recursion. Or, use the loop construct that actually has support for this kind of information transfer:
guess :: IO ()
guess = do
putStrLn "Please input your guess."
whileJust_
(do
lastGuess <- getLine -- don't use the name `guess` if that's already a global function name!
return $ if lastGuess /= "secret"
them Just lastGuess else Nothing )
(\lastGuess -> do
putStrLn $ "You guessed: " ++ lastGuess
putStrLn $ "And " ++ lastGuess ++ " is wrong." )
putStrLn "Right - Bye..."
However you can also communicate values between the condition- and execution parts of whileM_. Two options:
Specifically in IO, you can always use IORefs.
guess :: IO ()
guess = do
putStrLn "Please input your guess."
bestGuess <- newIORef ""
whileM_
(do lastGuess <- getLine
writeIORef bestGuess lastGuess
return $ lastGuess /= "secret" )
(do lastGuess <- readIORef bestGuess
putStrLn $ "You guessed: " ++ lastGuess
putStrLn $ "And " ++ lasyGuess ++ " is wrong." )
putStrLn "Right - Bye..."
This is rather eschewed in Haskell – an IORef is basically a mutable variable – but sometimes it's sensible. Definitely not a good idea here.
You can instead of IO use a dedicated monad with a pure-functional state variable. That requires a monad transformer. It's a slightly advanced technique, but for complex application can work out extremely well.

Related

How do I show two inputs are the same or not

I'm trying to just compare two user inputs but I can't seem to get it working and constantly get parse errors. Any help will be appreciated.
main = do
foo <- putStrLn "Enter two numbers."
numone <- getLine
numtwo <- getLine
putStrLn $ ("You entered " ++ numone ++ " and " ++ numtwo)
if
numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
The errors probably arise through to small changes in indentation between the local version, and the one posted here. Indentation in Haskell is quite important, since the compiler uses it to understand where certain "blocks" begin and end.
Furthermore you can remove the foo <- part (well this is not wrong, but quite useless). So after reformatting we get:
main = do
putStrLn "Enter two numbers."
numone <- getLine
numtwo <- getLine
putStrLn $ ("You entered " ++ numone ++ " and " ++ numtwo)
if numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
Furthermore now you compare two strings. You can convert these to Ints (or other readable types) with for example readLn :: Read a => IO a:
main = do
putStrLn "Enter two numbers."
numone <- readLn :: IO Int
numtwo <- readLn :: IO Int
putStrLn $ ("You entered " ++ show numone ++ " and " ++ show numtwo)
if numone == numtwo
then
putStrLn "They are the same"
else
putStrLn "They are not the same"
You have mixed tabs and spaces in your code snippet, and the blank line in between your print and your if expression is indented by less than the other lines are. Your whole do-block must have the same initial indentation. I suggest using only spaces (or only tabs, if you prefer) so that it's harder to accidentally wind up with misaligned code that looks correctly aligned.
I see I answered on the basis of code OP never wrote, because of an incorrect edit someone else made. It "fixed" the indentation but was actually still wrong for a different reason. Oh well, it's still an indentation problem but not one to do with mixing spaces and tabs.

Haskell - Do while loop

I'm new to Haskell and would be glad if someone would be willing to help me! I'm trying to get this program to work with a do while loop.
The result from the second getLine command gets put into the varible goGlenn and if goGlenn doesn't equal "start" then the program will return to the beginning
start = do
loop $ do lift performAction
putStrLn "Hello, what is your name?"
name <- getLine
putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory.")
putStrLn "You will receive fifty questions in total to which you can reply with Yes or No."
putStrLn "Whenever you feel ready to begin please write Start"
goGlenn <- getLine
putStrLn goGlenn
while (goGlenn /= "start")
In Haskell you write "loops" recursively, most of the times.
import Control.Monad
-- ....
start = do
putStrLn "Before the loop!"
-- we define "loop" as a recursive IO action
let loop = do
putStrLn "Hello, what is your name?"
name <- getLine
putStrLn $ "Welcome to our personality test " ++ name
++ ", inspired by the Big Five Theory."
putStrLn "You will receive fifty questions in total to which you can reply with Yes or No."
putStrLn "Whenever you feel ready to begin please write Start"
goGlenn <- getLine
putStrLn goGlenn
-- if we did not finish, start another loop
when (goGlenn /= "start") loop
loop -- start the first iteration
putStrLn "After the loop!"
Not sure, maybe this version can helps you:
import Control.Monad
loop action = do
condition <- action
when condition (loop action)
while = return
start =
let action = do {
putStrLn "Hello, what is your name?";
name <- getLine;
putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory.");
putStrLn "You will receive fifty questions in total to which you can reply with Yes or No.";
putStrLn "Whenever you feel ready to begin please write Start";
goGlenn <- getLine;
putStrLn goGlenn;
while (goGlenn /= "start");
}
in loop action
(Edit) or can be too:
start =
loop (do {
putStrLn "Hello, what is your name?";
name <- getLine;
putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory.");
putStrLn "You will receive fifty questions in total to which you can reply with Yes or No.";
putStrLn "Whenever you feel ready to begin please write Start";
goGlenn <- getLine;
putStrLn goGlenn;
while (goGlenn /= "start");
})

Haskell desugar syntax

Shuklan's Haskell Lecture wanted the following code desugared:
main = do
putStrLn "Enter name:"
name <- getLine
putStrLn ("Hi " ++ name)
I came up with:
main = putStrLn "Enter name:" >> getLine >>= \str -> putStrLn ("Hi " ++ str)
He revealed:
main = putStrLn "Enter name:" >> getLine >>= putStrLn . ("Hi " ++)
Never seen that syntax before, how does it work?
The snippets are identical, the latter just uses point free style (also punningly referred to as "pointless style").
The central point is that ("Hi " ++) is a partially applied (++) that prepends "Hi " to the input.
This function is composed (using .) with putStrLn to get a function that prepends "Hi " to the input and then prints it.
This is exactly what your more explicit lambda does.

Can I be sure of order of IO actions in this example?

At the moment, I have this code in and around main:
import Control.Monad
import Control.Applicative
binSearch :: Ord a => [a] -> a -> Maybe Int
main = do
xs <- lines <$> readFile "Cars1.txt"
x <- getLine <* putStr "Registration: " -- Right?
putStrLn $ case binSearch xs x of
Just n -> "Found at position " ++ show n
Nothing -> "Not found"
My hope is for “Registration: ” to be printed, then for the program to wait for the input to x. Does what I've written imply that that will be the case? Do I need the <*, or will putting the putStr expression on the line above make things work as well?
PS: I know I have to convert binSearch to work with arrays rather than lists (otherwise it's probably not worth doing a binary search), but that's a problem for another day.
The line
x <- getLine <* putStr "Registration: "
orders the IO actions left-to-right: first a line is taken as input, then the message is printed, and finally variable x is bound to the result of getLine.
Do I need the <*, or will putting the putStr expression on the line
above make things work as well?
If you want the message to precede the input, you have to put the putStr on the line above, as follows:
main :: IO ()
main = do
xs <- lines <$> readFile "Cars1.txt"
putStr "Registration: "
x <- getLine
putStrLn $ case binSearch xs x of
Just n -> "Found at position " ++ show n
Nothing -> "Not found"
Alternatively,
x <- putStr "Registration: " *> getLine
or
x <- putStr "Registration: " >> getLine
would work, but they are less readable.
Finally, since you added the lazy-evaluation tag, let me add that your question is actually not about laziness, but about how the operator <* is defined, and in particular about the order in which it sequences the IO actions.

The last statement in a 'do' construct must be an expression Haskell

Based on other similar questions I found I figure my problem has to do with indentation, but I've messed with it a lot and still can't figure it out.
addBook = do
putStrLn "Enter the title of the Book"
tit <- getLine
putStrLn "Enter the author of "++tit
aut <- getLine
putStrLn "Enter the year "++tit++" was published"
yr <- getLine
In your case it's not indentation; you really have finished your function with something that is not an expression. yr <- getLine — what did you expect to happen to yr, or for that matter aut, after this? They're just dangling, unused.
It may be clearer to show how this translates:
addBook = putStrLn "Enter the title of the Book" >>
getLine >>= \tit ->
putStrLn "Enter the author of "++ tit >>
getLine >>= \aut ->
putStrLn "Enter the year "++tit++" was published" >>
getLine >>= \yr ->
So, what did you want to have following that last arrow?
Think about the type of addBook. It's IO a where a is... nothing. That doesn't work. Your monad must have some result.
You might want to add something like this at the end:
return (tit, aut, yr)
Alternatively, if you don't want to have any useful result, return an empty tuple (a unit):
return ()
If you take your code:
addBook = do
putStrLn "Enter the title of the Book"
tit <- getLine
putStrLn "Enter the author of "++tit
aut <- getLine
putStrLn "Enter the year "++tit++" was published"
yr <- getLine
and "translate" it to "normal" (non-do) notation (given p = putStrLn "..."):
addBook =
p >> getLine >>= (\tit ->
p >> getLine >>= (\aut ->
p >> getLine >>= (yr ->
You are ending up with (yr -> that doesn't make sense. If you don't have anything else useful to do, you can just return an empty tuple:
return ()
at the end:
addBook = do
putStrLn "Enter the title of the Book"
tit <- getLine
putStrLn "Enter the author of "++tit
aut <- getLine
putStrLn "Enter the year "++tit++" was published"
yr <- getLine
return ()
You should probably ask yourself why you need to get aut and yr though.
remove the last line since it's not an expression,
then use parenthesis for the strings you pass to putStrLn.

Resources