2016-02-29 65 views
3

此代碼提供了以下編譯錯誤:爲什麼這個縮進錯了?

Error:(17, 1) ghc: parse error (possibly incorrect indentation or mismatched brackets) 

,但如果我刪除

module Main where 

它的工作原理。由於我剛開始使用Haskell,我想知道爲什麼?

module Main where 

{-# LANGUAGE QuasiQuotes #-} 

import Text.Hamlet (shamlet) 
import Text.Blaze.Html.Renderer.String (renderHtml) 
import Data.Char (toLower) 
import Data.List (sort) 

data Person = Person 
    { name :: String 
    , age :: Int 
    } 

main :: IO() 
main = putStrLn $ renderHtml [shamlet| 
<p>Hello, my name is #{name person} and I am #{show $ age person}. 
<p> 
    Let's do some funny stuff with my name: # 
    <b>#{sort $ map toLower (name person)} 
<p>Oh, and in 5 years I'll be #{show ((+) 5 (age person))} years old. 
|] 
    where 
    person = Person "Michael" 26 

回答

10

{-# LANGUAGE QuasiQuotes #-} 

應該來就在程序中的第一行,之前

module Main where 

這些語言擴展應該是元信息,外部程序本身(它們也可以作爲ghc的命令行選項)。

+3

這個。如果存在'LANGUAGE'編譯指示,它必須始終是文件中的第*行代碼。 (有可能是其他人,但我想不出任何副手。) – MathematicalOrchid

+1

@MathematicalOrchid @它可以跟蹤評論和空白字符串,它只是第一個有意義的行。 (所以它不像限制那樣嚴格)。 –

+1

實際上,'LANGUAGE' pragmas和'OPTIONS_GHC' pragmas必須作爲文件中第一個非註釋非空白的東西在塊中出現。你當然可以有多條這樣的線路,而且它們不能都是第一個:-) –

相關問題