0

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?

0

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Just to confirm, the OP's problem is that they're basically trying to define main inside the do block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.