I'm kind of a beginner when it comes to Haskell. Specifically with
regards to IO.
I'm getting close, but it's still asking for input after I enter
"done". I can't seem to get mapM to be lazy in the way I want.
On 11 Dec 2018, kfjwheeler wrote:
I'm kind of a beginner when it comes to Haskell. Specifically with
regards to IO.
You're making a promising start!
(snip)
I'm getting close, but it's still asking for input after I enter
"done". I can't seem to get mapM to be lazy in the way I want.
You're doing the three IO actions in sequence then selecting from their results. Broadly, with IO you don't get to skip stuff after the >>= just
by not consuming the result. So, you need to move the check of the input value amid the sequence itself, only do further IO actions if necessary.
You could try rolling that yourself (do another >>= only if necessary)
or use the help of something like unfoldM, we'd be happy to help you
with either.
-- Mark
On 11 Dec 2018, kfjwheeler wrote:
I'm kind of a beginner when it comes to Haskell. Specifically with
regards to IO.
You're making a promising start!
(snip)
I'm getting close, but it's still asking for input after I enter
"done". I can't seem to get mapM to be lazy in the way I want.
You're doing the three IO actions in sequence then selecting from their results. Broadly, with IO you don't get to skip stuff after the >>= just
by not consuming the result. So, you need to move the check of the input value amid the sequence itself, only do further IO actions if necessary.
You could try rolling that yourself (do another >>= only if necessary)
or use the help of something like unfoldM, we'd be happy to help you
with either.
-- Mark
mapM (\n -> do
putStrLn $"Input number " ++ show n ++ ": "
l <- getLine
return l)
[1..3]
mapM putStrLn $ words
I think the following is a much better way of doing while-loop-like constructs:
On 2018-12-11 05:19:18 -08, kfjwheeler@gmail.com wrote:
I'm kind of a beginner when it comes to Haskell. Specifically with
regards to IO.
I'm tying to write a very simple program that asks the user to input
a list of words. It gets words until the user enters "done".
I'm getting close, but it's still asking for input after I enter
"done". I can't seem to get mapM to be lazy in the way I want.
IO can be lazy, but the order of actions needs to preserved, so that
each putStrLn prompt precedes it's getLine.
I'm kind of a beginner when it comes to Haskell. Specifically with regards to IO.
I'm tying to write a very simple program that asks the user to input a list of words. It gets words until the user enters "done".
I'm getting close, but it's still asking for input after I enter "done". I can't seem to get mapM to be lazy in the way I want.
Oops, I was wrong. If I replace the:
xs <- getWordList 1
with:
xs <- take 3 `fmap` getWordList 1
the values continue to be read. So the IO can't be read lazily as far
as I can tell.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 286 |
Nodes: | 16 (2 / 14) |
Uptime: | 90:51:01 |
Calls: | 6,496 |
Calls today: | 7 |
Files: | 12,100 |
Messages: | 5,277,686 |