HXT:基于列表向树添加元素

时间:2018-02-06 01:34:23

标签: haskell hxt

我正在努力使用HXT库中的箭头基于列表(下面带有mapM的行)在树中生成元素。

mkqelem (mkQName vns "depositInstruction" pacNS) []  
    [selem "key" 
        [selem "accountId" [txt $ pacuAccountId pacUpdate],
         selem "instructionId" [txt $ pacuInstructionId pacUpdate]
        ],
     selem "totalAmount" [txt $ pacuTotalAmount pacUpdate],
     mapM mkInvestment  [(120, 10.0)]
     ]

mkInvestment :: ArrowXml a => (Fund, Amount) -> a n XmlTree
mkInvestment x = selem "investments" [selem "investmentId" [txt $ show $ fst x],selem "amount" [txt $ show $ snd x]] 

程序无法编译,我得到以下内容:

• Couldn't match type ‘[]’ with ‘XN.NTree’
  Expected type: a n XmlTree
    Actual type: a n [XmlTree]
• In the expression: mapM mkInvestment [(120, 10.0)]
  In the third argument of ‘mkqelem’, namely
    ‘[mkelem
        "key" [] [selem "accountId" [...], selem "instructionId" [...]],
      selem "totalAmount" [txt $ pacuTotalAmount pacUpdate],
      mapM mkInvestment [(120, 10.0)]]’
  In the expression:
    mkqelem
      (mkQName vns "depositInstruction" pacNS)
      []
      [mkelem
         "key" [] [selem "accountId" [...], selem "instructionId" [...]],
       selem "totalAmount" [txt $ pacuTotalAmount pacUpdate],
       mapM mkInvestment [(120, 10.0)]]

我尝试用+ =的替换替换mapM,但我对Arrows中的映射没有很好的直觉。有什么指针吗?

2 个答案:

答案 0 :(得分:1)

您的热门代码列表中有一个逗号,该代码在GHC在错误消息中打印的代码中不存在。如果没有逗号,GHC会尝试将mapMmkInvestment[(120, 10.0)]解释为selem的三个参数。

我没有尝试用逗号编译;我不知道是否还有其他错误。

答案 1 :(得分:1)

您的问题实际上并不涉及箭头;相反,它与mapM在这里不必要有关。在元组列表上创建元素箭头的简单fmap,然后将结果附加到包含其余元素的列表中,应该足够了。

mkqelem (mkQName vns "depositInstruction" pacNS) [] $
    [selem "key" 
        [selem "accountId" [txt $ pacuAccountId pacUpdate],
         selem "instructionId" [txt $ pacuInstructionId pacUpdate]
        ],
     selem "totalAmount" [txt $ pacuTotalAmount pacUpdate]
     ]
    ++ fmap mkInvestment [(120, 10.0), (121, 15.0)]

对于(+=),它会将子项添加到节点,因此您不会使用它而不是mapM / fmap,而是例如定义{{1}以不同的方式:

mkInvestment