gpt4 book ai didi

haskell - 无法将类型 ‘[Char]’ 与 ‘Data.Text.Internal.Text’ 匹配

转载 作者:行者123 更新时间:2023-12-03 23:06:01 24 4
gpt4 key购买 nike

这似乎是一个热门问题。我对 Haskell 一点都不熟悉,所以通过阅读类似问题的答案,我无法真正理解在我的案例中需要做什么。

我的脚本是这样的:

import Text.Pandoc.JSON

pagebreakXml :: String
pagebreakXml = "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>"

pagebreakBlock :: Block
pagebreakBlock = RawBlock (Format "openxml") pagebreakXml

blockSwapper :: Block -> Block
blockSwapper (Para [Str "<div class=\"docxPageBreak\"></div>"]) = pagebreakBlock
blockSwapper blk = blk

main = toJSONFilter blockSwapper

编译它会导致这些错误:

$ ghc --make docx-page-filter.hs -package-db=/Users/eugene/.cabal/store/ghc-8.8.3/package.db
[1 of 1] Compiling Main ( docx-page-filter.hs, docx-page-filter.o )

docx-page-filter.hs:7:35: error:
• Couldn't match expected type ‘Data.Text.Internal.Text’
with actual type ‘[Char]’
• In the first argument of ‘Format’, namely ‘"openxml"’
In the first argument of ‘RawBlock’, namely ‘(Format "openxml")’
In the expression: RawBlock (Format "openxml") pagebreakXml
|
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
| ^^^^^^^^^

docx-page-filter.hs:7:46: error:
• Couldn't match type ‘[Char]’ with ‘Data.Text.Internal.Text’
Expected type: Data.Text.Internal.Text
Actual type: String
• In the second argument of ‘RawBlock’, namely ‘pagebreakXml’
In the expression: RawBlock (Format "openxml") pagebreakXml
In an equation for ‘pagebreakBlock’:
pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
|
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
| ^^^^^^^^^^^^

docx-page-filter.hs:10:25: error:
• Couldn't match expected type ‘Data.Text.Internal.Text’
with actual type ‘[Char]’
• In the pattern: "<div class=\"docxPageBreak\"></div>"
In the pattern: Str "<div class=\"docxPageBreak\"></div>"
In the pattern: [Str "<div class=\"docxPageBreak\"></div>"]
|
10 | blockSwapper (Para [Str "<div class=\"docxPageBreak\"></div>"]) = pagebreakBlock
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$

起初,我接受了这里建议的可接受答案:Couldn't match expected type `Text' with actual type `[Char]' .它有助于将错误数量减少到一个错误。现在编译脚本会导致这个错误,我不知道如何修复:

$ ghc --make docx-page-filter.hs -package-db=/Users/eugene/.cabal/store/ghc-8.8.3/package.db -XOverloadedStrings
[1 of 1] Compiling Main ( docx-page-filter.hs, docx-page-filter.o )

docx-page-filter.hs:7:46: error:
• Couldn't match type ‘[Char]’ with ‘Data.Text.Internal.Text’
Expected type: Data.Text.Internal.Text
Actual type: String
• In the second argument of ‘RawBlock’, namely ‘pagebreakXml’
In the expression: RawBlock (Format "openxml") pagebreakXml
In an equation for ‘pagebreakBlock’:
pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
|
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
| ^^^^^^^^^^^^
$

我应该如何更改我的代码以解决此问题?

最佳答案

在 Haskell 中有多种常用的字符串类型。默认情况下,字符串文字("stuff in quotes")的类型为 String(即 [Char]),但是您的库正在使用的期望值类型为 Text

  • 启用 OverloadedStrings 扩展,这样字符串文字也可以具有类型 Text

  • 将类型签名从 String 更改为 TextText 可以从 text 库中的 Data.Text 模块导入(还值得一提的是,有两个 Text,另一个来自 Data.Text.Lazy,这可能是您将来不匹配的另一个来源)。

{-# LANGUAGE OverloadedStrings #-}   -- Add at the top of the file

... -- imports
import Data.Text (Text) -- Import the Text type

pagebreakXml :: Text -- from String to Text
pagebreakXml = "<w:p>..."

关于haskell - 无法将类型 ‘[Char]’ 与 ‘Data.Text.Internal.Text’ 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62679154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com