I want to ask the user for a sentence, and then return the sentence back but with the words on separate lines.
For example, if the user inputs "hello I am tall"
, the computer returns:
hello,
I
am
tall
I tried to start off a bit, but don't know of a function or something I can use to try and separate the sentence. My code so far:
displayWords ::IO ()
displayWords = do putStr "Please enter a line of text"
x <- getLine
mapM print x
I get the error:
Couldn't match type ‘[()]’ with ‘()’
EDIT: One more side thing... using mapM_ print (words x)
fixes what I want, but is there a way to print this without the quotation marks?
EDIT2: One more thing... Someone in the comments helped answer the previous edit, but if I change the format of this to
displayWords:: String -> IO Int()
displayWords s = do
mapM_ putStrLn s
return (length s)
I get the error
Couldn't match type 'Char' With '[Char]'
How come putStrLn
doesn't work here?
You need the words
function to split the string into separate words.
Someone suggested mapM_ print (words x)
, but since each word is a string, using print
will wrap it in quote marks, which you don't want. So try
mapM_ putStrLn (words x)