私は、矢印を使用するいくつかの Haskell コードのよりコンパクトなバージョンを作成しようとしています。
xml をタプルのリストに変換しようとしています。
tx2 を実行すると、[("Item 1","Item One",["p1_1","p1_2","p1_3"]),("Item 2","Item Two",["p2_1","p2_2") が生成されます。 ])]
私が持っているコードは機能しますが、runLA 呼び出しをそれほど多く使用する必要はないと考えずにはいられません。getDesc、getDisp、getPlistのそれぞれに対して runLA を呼び出します。
簡略化するためにprocとdo表記を使用できるのではないかと考えました
{-# 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
しかし、次のように 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
それはすべて大きな山に落ちます。
proc/do 表記への変換で何が欠けていますか?
ありがとう。