let test2 =
(fromDocument (doc :: Document) $/ element "body")
>>= ($/ element "div")
>>= (attributeIs "id" "content")
>>= ($/ element "div")
>>= (attributeIs "class" "box")我使用的是xml-conduit库,我得到了上面的代码,它实际上过滤了一棵树(一棵XML )。
如果任何绑定(>>=) (如果正确的话)返回一个空列表,我想将功能添加到"error“中。
我想做的事:
element和attributeIs都返回Axis,即Cursor -> [Cursor]
我发现很难确定我需要使用的数据类型。我正在考虑使用Either来返回Left a (a将是一个类似于attributeIs "id" "content"的函数)或Right Axis。
但是,如果我的理解是正确的,我就无法返回列表中的Either类型。
发布于 2016-09-23 11:30:28
如果它用于快速调试,只需使用Debug.Trace即可。否则,您将请求合并monads、List和Either String:这是一个monad变压器。考虑下面这个关于整数而不是XML树的简单示例:
plusOne :: Int -> [Int]
plusOne x = [x+1]
void :: Int -> [Int]
void x = if x <= 2 then [x] else []
listPipe :: [Int]
listPipe = [1,2,3] >>= plusOne >>= voidlistPipe中的一些值是由void裁剪的,您想知道这在运行时何时发生。然后你的类型变成:
plusOne :: Int -> Either String [Int]
void :: Int -> Either String [Int]
listPipe :: Either String [Int]它与ListT (Either String) Int类型同构。包装给你的是:
import Control.Monad.Trans.List
void :: Int -> ListT (Either String) Int
void x = ListT $ if x <= 2 then Right [x] else Left "errorVoid"
plusOne :: Int -> ListT (Either String) Int
plusOne x = ListT $ Right [x+1]
listPipe :: Either String [Int]
listPipe = runListT $ ListT (Right [1,2,3]) >>= plusOne >>= void和listPipe = Left "errorVoid"。
https://stackoverflow.com/questions/39649028
复制相似问题