Haskell得到了来自Get monad的价值

时间:2016-12-22 15:31:16

标签: haskell functional-programming monads

我是一个Haskell noob感觉坚持我试图执行的一个相当简单的功能。最终我的目标是阅读严格的ByteString,使用带有解码器的Get monad从Word32中检索第一个ByteString,并执行{{1}的特定函数在Data.Bits.testBit的各个部分评估为True。

这是我的示例代码:

Word32

当我在解码器上运行import Data.List import Data.Char import Data.Function import System.Random import Data.Bits import Data.Either import Data.Binary.Strict.Get import System.IO as SIO import Data.ByteString.Char8 as B import Data.Word (Word32) import Data.ByteString.UTF8 as BU dateTemplate = "YYMMDDhhmmss" convertFromString :: String -> ByteString convertFromString s = BU.fromString s mahDecoder :: Get Word32 mahDecoder = do first32Bits <- getWord32be return first32Bits main :: IO () main = do let a = runGet mahDecoder (convertFromString dateTemplate) SIO.putStrLn $ show a -- When I uncomment these lines I get the problem --case a of -- Left val -> SIO.putStrLn "Communist!" -- Right val -> SIO.putStrLn $ "Fascist!" 函数并在main中传递runGet时,我可以看到它返回一个Either实例,如下所示:

ByteString

当我试图左或右时,它失败并出现以下错误:

(Right 1499024717,"DDhhmmss")

我在这里做错了什么想法?我觉得我应该用解码器读取ByteString中的所有字节。说实话,我不完全确定HSStackOverflowExamp.hs:31:5: Couldn't match expected type `(Either String Word32, ByteString)' with actual type `Either t0 t1' In the pattern: Left val In a case alternative: Left val -> SIO.putStrLn "Communist!" In a stmt of a 'do' block: case a of { Left val -> SIO.putStrLn "Communist!" Right val -> SIO.putStrLn $ "Fascist!" } 的类型是什么。我对Monads和Monad变形金刚的理解仍然非常有限。我希望在实现简单的练习用例之前,我不需要掌握每一个Haskell黑暗艺术。如果答案是继续阅读,那么我接受。

1 个答案:

答案 0 :(得分:2)

var age = parseInt(document.getElementById("ageVerify").value); 的类型是第一项是var transitions = [{ $Duration: 1200, x: 0.3, $During: { $Left: [0.3, 0.7] }, $Easing: { $Left: $Jease$.$InCubic, $Opacity: $Jease$.$Linear}, $Opacity: 2} ]; var options = { $AutoPlay: false, $SlideWidth: 75, $SlideHeight: 75, $SlideshowOptions: { $Class: $JssorSlideshowRunner$, $Transitions: transitions, $TransitionsOrder: 1, }, $Cols: 5, $Loop:0 }; 的元组。尝试将您的案例陈述更改为:

a