【问题标题】:Haskell HappstackHaskell Happstack
【发布时间】:2015-04-15 10:15:29
【问题描述】:

尝试使用 happstack,让它在 Windows 上正确安装,但现在当我编译我的测试类时,它会排除一些错误,任何输入都将不胜感激。

module Main where

import Happstack.Server
import           Text.Blaze ((!))
import qualified Text.Blaze.Html4.Strict as H
import qualified Text.Blaze.Html4.Strict.Attributes as A

appTemplate :: String -> [H.Html] -> H.Html -> H.Html
appTemplate title headers body =
H.html $ do
  H.head $ do
    H.title (H.toHtml title)
    H.meta ! A.httpEquiv "Content-Type"
           ! A.content "text/html;charset=utf-8"
    sequence_ headers
  H.body $ do
    body

helloBlaze :: ServerPart Response
helloBlaze =
ok $ toResponse $
appTemplate "Hello, Blaze!"
            [H.meta ! A.name "keywords"
                    ! A.content "happstack, blaze, html"
            ]
            (H.p $ do "Hello, "
                      H.b "blaze-html!")

main :: IO ()
main = simpleHTTP nullConf $ helloBlaze

我创建了一个 happstack 文件夹并使用 cabal 将所需的文件安装到该文件夹​​中,但是当我编译代码时出现以下错误。

Main.hs:13:30:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.httpEquiv’, namely ‘"Content-Type"’
In the second argument of ‘(!)’, namely
  ‘A.httpEquiv "Content-Type"’
In the first argument of ‘(!)’, namely
  ‘H.meta ! A.httpEquiv "Content-Type"’

Main.hs:14:28:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.content’, namely
  ‘"text/html;charset=utf-8"’
In the second argument of ‘(!)’, namely
  ‘A.content "text/html;charset=utf-8"’
In a stmt of a 'do' block:
  H.meta ! A.httpEquiv "Content-Type"
  ! A.content "text/html;charset=utf-8"

Main.hs:23:34:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.name’, namely ‘"keywords"’
In the second argument of ‘(!)’, namely ‘A.name "keywords"’
In the first argument of ‘(!)’, namely ‘H.meta ! A.name "keywords"’

Main.hs:24:37:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.content’, namely
  ‘"happstack, blaze, html"’
In the second argument of ‘(!)’, namely
  ‘A.content "happstack, blaze, html"’
In the expression:
  H.meta ! A.name "keywords" ! A.content "happstack, blaze, html"

Main.hs:26:27:
 Couldn't match type ‘[]’ with ‘Text.Blaze.Internal.MarkupM’

 Expected type: Text.Blaze.Internal.MarkupM Char
   Actual type: [Char]
 In a stmt of a 'do' block: "Hello, "
 In the second argument of ‘($)’, namely
   ‘do { "Hello, ";
         H.b "blaze-html!" }’
 In the third argument of ‘appTemplate’, namely
   ‘(H.p
     $ do { "Hello, ";
            H.b "blaze-html!" })’

Main.hs:27:31:
Couldn't match type ‘[Char]’ with ‘Text.Blaze.Internal.MarkupM ()’
Expected type: H.Html
  Actual type: [Char]
In the first argument of ‘H.b’, namely ‘"blaze-html!"’
In a stmt of a 'do' block: H.b "blaze-html!"
In the second argument of ‘($)’, namely
  ‘do { "Hello, ";
        H.b "blaze-html!" }’

【问题讨论】:

  • 您是否包含了OverloadedStrings 扩展名(将其添加到文件顶部{-# LANGUAGE OverloadedStrings #-} 或使用命令行参数-XOverloadedStrings)?
  • {-# LANGUAGE OverloadedStrings #-}
  • 就是这样吗?在模块之前还是之后 Main 在哪里?

标签: haskell happstack


【解决方案1】:

在这样的行中:

Couldn't match expected type ‘H.AttributeValue’
        with actual type ‘[Char]’

编译器抱怨它希望收到H.AttributeValue,但收到了[Char]

AttributeValue 定义为 here。如您所见,它是IsString 的一个实例,这意味着您可以使用OverloadedStrings,这是一个语言扩展。启用它放

{-# LANGUAGE OverloadedStrings #-}

在文件的顶部。

这将像这样重写片段:

A.httpEquiv "Content-Type"

到这样的事情:

A.httpEquiv (fromString "Content-Type")

fromString 来自IsString 定义here 的类,如:

class IsString a where
    fromString :: String -> a

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多