0

我正在努力使用 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 中的映射没有很好的直觉。任何指针?

4

2 回答 2

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,而是例如以mkInvestment不同的方式定义:

mkInvestment :: ArrowXml a => (Fund, Amount) -> a n XmlTree
mkInvestment x = eelem "investments"
    += (eelem "investmentId" += (txt $ show $ fst x))
    += (eelem "amount" += (txt $ show $ snd x))
    -- Minor style suggestion: you might prefer to use pattern matching here 
    -- instead of fst and snd.
于 2018-02-09T13:45:31.007 回答
1

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

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

于 2018-02-06T19:53:43.197 回答