2017-04-25 13 views
0

類似しているが全く同じではないエラーをいくつか検索した後で、この問題をデバッグする際に次のステップに進むことができません。 this Haskell scriptPandoc returnedどちらかのタイプ

import Text.Pandoc (writePlain, readHtml, def, pandocVersion) 

convert :: String -> String 
convert = writePlain def . readHtml def 

から

関連する行は、このエラーが発生しています:

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
  • と4.10.9

cabal installをされてたのx

  • アーチは、以下のような解決策を働いて、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 
    
  • 答えて

    2

    作成しようとしている2つの関数のタイプを見てみましょう:

    readHtml:: ReaderOptions Reader options -> String -> Either PandocError Pandoc

    readHtmlは失敗する操作です。これを表現するために、を返します。 a PandocErrorまたは有効なPandocです。

    writePlain:: WriterOptions -> Pandoc -> String

    writePlainのみ有効Pandocを期待しています。

    プログラムは両方のケースを処理する必要があります。

    1. readHtml
    2. 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 
    

    Either a bは、慣れている場合はMaybe aに似ていますが、障害が発生した場合には追加情報を持つことができます。慣例により、Leftコンストラクタはエラー値を表すために使用され、Rightコンストラクタは通常値を表すために使用されます。

    +1

    'convert'の型シグネチャを' String - > PandocError Strings 'に変更したほうがよいので、この 'convert'関数の呼び出し側はエラーに反応するための妥当な手順をとることができます。型システムは、 'String - > String'がこの関数の良い型ではないことを警告しようとしています。さらなる証拠:あなたは 'show'を 'Left'の場合に書かなければなりませんでしたが、これは明らかにこの関数の良い結果ではありません。 – amalloy