This function allows the user to input a list of strings. The function takes the length and allows the user to input length-1 more lines. Then each line is checked to ensure it is the same length as the original line. The code:
readme :: IO [Line]
readme = do
line <- readLn
let count = length line
lines <- replicateM (count-1) $ do
line <- readLn
if length line /= count
then fail "too long or too short"
else return line
return $ line : lines
Line is of type String.
When I try to run the function and input.. say [“12″,”13”] I get the following: * Exception: user error (Prelude.readIO: no parse) and I can’t figure out why, any ideas?
It’s because you are trying to read something with the wrong type.
You say that
Lineis aStringaka.[Char]. However, the input you are typing is of the format["12", "13"]which looks like it should have type[Line], aka.[String]or[[Char]].You need to explain what a
Lineis actually supposed to be. If you want aLineto be a string, then why are you entering lists of strings at the terminal? Something is wrong with your logic in this case.If you want a method for inputting square matrices, you can let
type Line = [Int]instead, and use one of these formats:If you really want to input lines, so that
type Line = [Char], and that each number in the input list becomes a unicode character, meaning that when you enter[97, 98, 99]on the terminal, you get the string"abc":