Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 返回任意一种类型的Pandoc_Haskell_Pandoc - Fatal编程技术网

Haskell 返回任意一种类型的Pandoc

Haskell 返回任意一种类型的Pandoc,haskell,pandoc,Haskell,Pandoc,在搜索了一些类似但不完全相同的错误之后,我不知道下一步在哪里调试这个问题 相关线路来自 导致此错误的原因是: Main.hs:11:28: error: • Couldn't match type ‘Either Text.Pandoc.Error.PandocError Text.Pandoc.Definition.Pandoc’ with ‘Text.Pandoc.Definition

在搜索了一些类似但不完全相同的错误之后,我不知道下一步在哪里调试这个问题

相关线路来自

导致此错误的原因是:

Main.hs:11:28: error:
    • Couldn't match type ‘Either
                             Text.Pandoc.Error.PandocError Text.Pandoc.Definition.Pandoc’
                     with ‘Text.Pandoc.Definition.Pandoc’
      Expected type: String -> Text.Pandoc.Definition.Pandoc
        Actual type: String
                     -> Either
                          Text.Pandoc.Error.PandocError Text.Pandoc.Definition.Pandoc
    • In the second argument of ‘(.)’, namely ‘readHtml def’
      In the expression: writePlain def . readHtml def
      In an equation for ‘convert’:
          convert = writePlain def . readHtml def
环境详情:

  • GHC 8.0.1
  • 阴谋集团1.24
  • 拱x64 4.10.9
随着
pandoc
已经
cabal安装
'd

感谢您的回答、评论和几个小时的把头撞在墙上,工作解决方案如下:

import Network.HTTP.Conduit (simpleHttp)
import Data.Text.Lazy as TL
import Data.Text.Lazy.Encoding as TLE
import Text.Pandoc
import Text.Pandoc.Error
import Data.Set

htmlToPlainText :: String -> String
htmlToPlainText = writePlain (def {
    writerExtensions = Data.Set.filter (/= Ext_raw_html) (writerExtensions def)
  }) . handleError . readHtml def

main :: IO ()
main = do
    response <- simpleHttp "https://leonstafford.github.io"

    let body = TLE.decodeUtf8 ( response )
    let bodyAsString = TL.unpack ( body )

    putStrLn $ htmlToPlainText bodyAsString
import Network.HTTP.conductor(simpleHttp)
将Data.Text.Lazy导入为TL
将Data.Text.Lazy.Encoding导入为TLE
导入文本.Pandoc
导入Text.Pandoc.Error
导入数据集
HTML纯文本::字符串->字符串
htmlToPlainText=writePlain(def{
writerExtensions=Data.Set.filter(/=Ext_raw_html)(writerExtensions def)
}) . handleError。readHtml定义
main::IO()
main=do

response查看您尝试编写的两个函数的类型:

::读卡器选项读卡器选项->字符串->PandocError Pandoc

readHtml
是一个可能失败的操作。为了表示这一点,它返回一个PandocError或一个有效的Pandoc

::WriterOptions->Pandoc->String

writePlain
只需要有效的Pandoc

程序必须处理两种情况:

  • readHtml
    返回左/错误值
  • readHtml
    返回正确/有效的值
  • 这可以通过多种方式实现,但例如:

    import Text.Pandoc (writePlain, readHtml, def, pandocVersion)
    
    convert :: String -> String
    convert = case readHtml def of
      Left err -> show err
      Right doc -> writePlain def doc
    

    如果您熟悉这一点,b
    或a类似于
    或a
    ,只是在发生故障时它可能有额外的信息。按照惯例,
    构造函数用于表示错误值,
    构造函数用于表示正常值。

    看看您试图组合的两个函数的类型:

    ::读卡器选项读卡器选项->字符串->PandocError Pandoc

    readHtml
    是一个可能失败的操作。为了表示这一点,它返回一个PandocError或一个有效的Pandoc

    ::WriterOptions->Pandoc->String

    writePlain
    只需要有效的Pandoc

    程序必须处理两种情况:

  • readHtml
    返回左/错误值
  • readHtml
    返回正确/有效的值
  • 这可以通过多种方式实现,但例如:

    import Text.Pandoc (writePlain, readHtml, def, pandocVersion)
    
    convert :: String -> String
    convert = case readHtml def of
      Left err -> show err
      Right doc -> writePlain def doc
    

    如果您熟悉这一点,b
    或a类似于
    或a
    ,只是在发生故障时它可能有额外的信息。按照惯例,
    Left
    构造函数用于表示错误值,
    Right
    构造函数用于表示正常值。

    最好将
    convert
    的类型签名更改为
    String->PandocError String
    ,因此,此
    convert
    函数的调用方可以采取合理的步骤对错误作出反应。类型系统试图警告您,
    String->String
    不是适合此函数的类型。进一步的证据:您必须在
    Left
    案例中写入
    show err
    ,这显然不是此函数的好结果。最好将
    convert
    的类型签名更改为
    String->PandocError String
    ,因此,此
    convert
    函数的调用方可以采取合理的步骤对错误作出反应。类型系统试图警告您,
    String->String
    不是适合此函数的类型。进一步的证据:您必须在
    Left
    案例中编写
    show err
    ,这显然不是此函数的好结果。