2016-04-03 98 views
0

當我只寫main = do ...塊時,它完美的工作。當我把只有stories = do...塊它也可以。也許有一個縮進問題。解析錯誤輸入「=」haskell錯誤

下面是代碼:

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) 

有什麼不對呢?

回答

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) 
+1

只是爲了確認,在OP的問題是,他們基本上是試圖定義'了'do'塊內main'。 – BalinKingOfMoria