haskell箭头在尝试使用proc和do表示法时出现混淆
我一直在尝试编写一些使用箭头的Haskell代码的更紧凑版本 我正在尝试将xml转换为元组列表 运行tx2产生: [(“项目1”、“项目1”、“p1_1”、“p1_2”、“p1_3”])、(“项目2”、“项目2”、“p2_1”、“p2_2”])] 我拥有的代码可以工作,但我忍不住想,我不应该像我那样使用那么多runLA调用。我为getDesc、getDisp和getPlist中的每一个调用runLA 我想我可以使用proc和do符号来简化haskell箭头在尝试使用proc和do表示法时出现混淆,haskell,arrows,Haskell,Arrows,我一直在尝试编写一些使用箭头的Haskell代码的更紧凑版本 我正在尝试将xml转换为元组列表 运行tx2产生: [(“项目1”、“项目1”、“p1_1”、“p1_2”、“p1_3”])、(“项目2”、“项目2”、“p2_1”、“p2_2”])] 我拥有的代码可以工作,但我忍不住想,我不应该像我那样使用那么多runLA调用。我为getDesc、getDisp和getPlist中的每一个调用runLA 我想我可以使用proc和do符号来简化 {-# LANGUAGE Arrows, NoMonom
{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
module Test1 where
import Text.XML.HXT.Arrow.ReadDocument
import Text.XML.HXT.Core
xml = "<top>\
\<list>\
\<item>\
\<desc>Item 1</desc>\
\<plist>\
\<p>p1_1</p>\
\<p>p1_2</p>\
\<p>p1_3</p>\
\</plist>\
\<display>Item One</display>\
\</item>\
\<item>\
\<desc>Item 2</desc>\
\<plist>\
\<p>p2_1</p>\
\<p>p2_2</p>\
\</plist>\
\<display>Item Two</display>\
\</item>\
\</list>\
\</top>"
tx1 = runLA (xread >>> getChildren >>> hasName "list" >>> getChildren >>> hasName "item") xml
tx2 = map toTuple tx1
toTuple i = let
desc = getDesc i
display = getDisp i
plist = getPlist i
in (desc, display, plist)
aDesc = getChildren >>> hasName "desc" >>> getChildren >>> getText >>> unlistA
aDisp = getChildren >>> hasName "display" >>> getChildren >>> getText >>> unlistA
aPlist = getChildren >>> hasName "plist" >>> getChildren >>> deep getText
getDesc i = runLA aDesc i
getDisp i = runLA aDisp i
getPlist i = runLA aPlist i
{-#语言箭头,nomonomogormism限制#-}
模块Test1在哪里
导入Text.XML.HXT.Arrow.ReadDocument
导入Text.XML.HXT.Core
xml=”\
\\
\\
\项目1\
\\
\ p1_1\
\ p1_2\
\ p1_3\
\\
\项目一\
\\
\\
\项目2\
\\
\ p2_1\
\ p2_2\
\\
\项目二\
\\
\\
\"
tx1=runLA(xread>>>getChildren>>>hasName“list”>>>getChildren>>>hasName“item”)xml
tx2=映射到耦合tx1
totuplei=let
desc=getDesc i
display=getDisp i
plist=getplisti
输入(描述、显示、plist)
aDesc=getChildren>>>hasName“desc”>>>getChildren>>>getText>>>unlistA
aDisp=getChildren>>>hasName“显示”>>>getChildren>>>getText>>>unlistA
aPlist=getChildren>>>hasName“plist”>>>getChildren>>>深getText
getDesc i=runLA aDesc i
getDisp i=runLA aDisp i
getPlist i=runLA aPlist i
但当我试图重写tx2时,如下所示:
aToTuple = proc tree -> do
desc <- aDesc -< tree
display <- aDisp -< tree
plist <- aPlist -< tree
returnA -< (desc, display, plist)
tx3 = map (\i -> runLA aToTuple i) tx1
aToTuple=proc tree->do
desc您几乎不必在HXT上多次调用run
-函数
箭头以获得所需的结果。在您的情况下,可以使用,而不是
map runLA
从箭头获取结果列表。你也可以摆脱
许多getChildren
调用都是通过使用运算符进行的
您的proc
-版本的toTuple
在我看来很好,但我已经重写了其余部分
您的示例代码如下
tx1 = runLA (xread /> hasName "list" /> hasName "item" >>> toTuple) xml
toTuple = proc tree -> do
desc <- aDesc -< tree
disp <- aDisp -< tree
plist <- aPlist -< tree
returnA -< (desc, disp, plist)
aDesc = getChildren >>> hasName "desc" /> getText
aDisp = getChildren >>> hasName "display" /> getText
aPlist = getChildren >>> hasName "plist" >>> listA (getChildren /> getText)
您正在使用-XArrows
?@AndrewJaffe:代码示例的顶部有一个pragma。非常感谢,这正是我想要的,现在我要了解它在做什么。再次感谢
toTuple = aDesc &&& aDisp &&& aPlist >>> arr3 (,,)