I’ve always programmed on C++ and Pascal and think too imperatively. So, could anyone help me with the question:
Consider we have the following input pattern:
integer n
n strings
other data
For example:
2
foo
bar
3 4
and so on.
So, I need to read only n Strings into a List, without reading other data. How should I do that without for-like constructions?
One possible method is
getLineis an IO action that reads a line from the standard input and returns it as a string. Its type isIO String.replicate ncreates a list ofnidentical items. Soreplicate n getLineis a list ofnIO actions, each returning a string:[IO String].sequenceis a function that takes a list of actions that return something, and turns it into a single action that returns a list of that something. So if we have an[IO String], thensequencewill turn it intoIO [String].Which is just what we want.