当我只写main = do ...
块时,它完美无缺。当我只放置stories = do...
块时,它也有效。也许有一个缩进问题。
以下是代码:
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)
它出了什么问题?
答案 0 :(得分:2)
虽然这个事实并不像Python那样广告,但Haskell确实有syntactically significant indentation。在您的情况下,do
s主体中的代码必须缩进:
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)