0

I am trying to read a string from keyboard. This is my code so far:

getLine :: IO String
getLine = do x <- getChar if x=='\n' then return [] else do xs<-getLine return (x:xs)

The problem is that I get this error when compiling:

parse error on input 'if'

Any ideas what I am doing wrong?

1 Answer 1

3

If you're going to insist on writing everything on one line, you'll need to insert a semicolon between consecutive statements in do blocks:

getLine = do x <- getChar; if x=='\n' then return [] else do xs<-getLine; return (x:xs)
                         ^ here                                         ^ and here

It would be better to just split the whole thing across multiple lines, though:

getLine = do x <- getChar
             if x=='\n' then return []
                        else do xs<-getLine
                                return (x:xs)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.