List 如何将错误处理添加到列表monad?我需要什么数据类型?
我使用的是List 如何将错误处理添加到列表monad?我需要什么数据类型?,list,haskell,error-handling,List,Haskell,Error Handling,我使用的是xml管道库,上面的代码基本上过滤了一棵树(xml树) 如果任何绑定(>=)返回空列表,我想在“error”中添加功能 我想做什么: 元素和属性都返回轴,即光标->[Cursor] 我发现很难制定我需要使用的数据类型。 我正在考虑使用返回左a(a将是类似属性“id”“content”)或右轴的函数 但是如果我的理解正确,我也无法在列表monad中返回类型。如果是为了快速调试,只需使用Debug.Trace。否则,您要求组合monadList和任一字符串:这是一个错误。考虑整数上的这个
xml管道
库,上面的代码基本上过滤了一棵树(xml树)
如果任何绑定(>=
)返回空列表,我想在“error”中添加功能
我想做什么:
元素
和属性都返回轴
,即光标->[Cursor]
我发现很难制定我需要使用的数据类型。
我正在考虑使用返回左a
(a
将是类似属性“id”“content”
)或右轴的函数
但是如果我的理解正确,我也无法在列表monad中返回类型。如果是为了快速调试,只需使用Debug.Trace
。否则,您要求组合monadList
和任一字符串:这是一个错误。考虑整数上的这个简单的例子,而不是XML树:
let test2 =
(fromDocument (doc :: Document) $/ element "body")
>>= ($/ element "div")
>>= (attributeIs "id" "content")
>>= ($/ element "div")
>>= (attributeIs "class" "box")
它与类型ListT(任一字符串)Int
同构。通过包装,您可以:
plusOne :: Int -> Either String [Int]
void :: Int -> Either String [Int]
listPipe :: Either String [Int]
import Control.Monad.Trans.List
void::Int->ListT(任一字符串)Int
void x=ListT$if x ListT(任一字符串)Int
plusOne x=ListT$Right[x+1]
listPipe::任意字符串[Int]
listPipe=runListT$ListT(右[1,2,3])>>=plusOne>>=void
并且listPipe=Left“errorVoid”
它已经可以工作了。请记住,join
for list采用内部列表的笛卡尔积,因此如果其中任何一个列表为空,则生成的列表将为emptyCorrect,尽管我希望返回的不是空列表,而是一个包含至少1个轴的函数的列表。因此,最终目标是确定哪个“过滤器”导致整个“管道”线返回[]
。您应该将此添加到问题中。
plusOne :: Int -> Either String [Int]
void :: Int -> Either String [Int]
listPipe :: 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