2016-02-29 55 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必须作为文件中第一个非注释非空白的东西在块中出现。你当然可以有多条这样的线路,而且它们不能都是第一个:-) –

相关问题