Nika Tsogiaidze
Nika Tsogiaidze

Reputation: 1097

Parse error on input "=" haskell error

When I write only main = do ... block, it works perfectly. When I put only stories = do... block it also works. Maybe there is an indentation problem.

Here is the code:

stories = do
let str0 = "There once was "
str1 <- ["a princess ", "a cat ", "a little boy "]
let str2 = "who lived in "
return (  str0 ++ str1  )

main = do
let len = length stories
putStrLn ("Enter a number from 0 to " ++ show (len - 1))
n <- readLn
putStrLn ""
putStrLn (stories !! n)

What is wrong with it?

Upvotes: 0

Views: 52

Answers (1)

fjarri
fjarri

Reputation: 9726

Although this fact is not as advertised as in the case of, say, Python, Haskell does have syntactically significant indentation. In your case the code in the bodies of dos must be indented:

stories = do
    let str0 = "There once was "
    str1 <- ["a princess ", "a cat ", "a little boy "]
    let str2 = "who lived in "
    return (  str0 ++ str1  )

main = do
    let len = length stories
    putStrLn ("Enter a number from 0 to " ++ show (len - 1))
    n <- readLn
    putStrLn ""
    putStrLn (stories !! n)

Upvotes: 2

Related Questions