gpt4 book ai didi

haskell - 将箭头从箭头中分解出来是否是有效的转换?

转载 作者:行者123 更新时间:2023-12-02 04:35:51 25 4
gpt4 key购买 nike

我正在努力了解 HXT,这是一个用于解析使用箭头的 XML 的 Haskell 库。对于我的特定用例,我宁愿不使用 deep因为在某些情况下 <outer_tag><payload_tag>value</payload_tag></outer_tag>不同于<outer_tag><inner_tag><payload_tag>value</payload_tag></inner_tag></outer_tag>但我遇到了一些奇怪的事情,感觉它应该起作用但不起作用。

我设法想出一个基于 this example 的测试用例来自文档:

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
module Main where

import Text.XML.HXT.Core

data Guest = Guest { firstName, lastName :: String }
deriving (Show, Eq)


getGuest = deep (isElem >>> hasName "guest") >>>
proc x -> do
fname <- getText <<< getChildren <<< deep (hasName "fname") -< x
lname <- getText <<< getChildren <<< deep (hasName "lname") -< x
returnA -< Guest { firstName = fname, lastName = lname }

getGuest' = deep (isElem >>> hasName "guest") >>>
proc x -> do
fname <- getText <<< getChildren <<< (hasName "fname") <<< getChildren -< x
lname <- getText <<< getChildren <<< (hasName "lname") <<< getChildren -< x
returnA -< Guest { firstName = fname, lastName = lname }

getGuest'' = deep (isElem >>> hasName "guest") >>> getChildren >>>
proc x -> do
fname <- getText <<< getChildren <<< (hasName "fname") -< x
lname <- getText <<< getChildren <<< (hasName "lname") -< x
returnA -< Guest { firstName = fname, lastName = lname }


driver finalArrow = runX (readDocument [withValidate no] "guestbook.xml" >>> finalArrow)

main = do
guests <- driver getGuest
print "getGuest"
print guests

guests' <- driver getGuest'
print "getGuest'"
print guests'

guests'' <- driver getGuest''
print "getGuest''"
print guests''

getGuest 之间和 getGuest'我展开deep进入正确的数量 getChildren .结果函数仍然有效。然后我考虑 getChildrendo之外 block ,但这会导致生成的函数失败。输出是:

"getGuest"
[Guest {firstName = "John", lastName = "Steinbeck"},Guest {firstName = "Henry", lastName = "Ford"},Guest {firstName = "Andrew", lastName = "Carnegie"},Guest {firstName = "Anton", lastName = "Chekhov"},Guest {firstName = "George", lastName = "Washington"},Guest {firstName = "William", lastName = "Shakespeare"},Guest {firstName = "Nathaniel", lastName = "Hawthorne"}]
"getGuest'"
[Guest {firstName = "John", lastName = "Steinbeck"},Guest {firstName = "Henry", lastName = "Ford"},Guest {firstName = "Andrew", lastName = "Carnegie"},Guest {firstName = "Anton", lastName = "Chekhov"},Guest {firstName = "George", lastName = "Washington"},Guest {firstName = "William", lastName = "Shakespeare"},Guest {firstName = "Nathaniel", lastName = "Hawthorne"}]
"getGuest''"
[]

我觉得这应该是一个有效的转换,但我对箭头的理解有点不稳定。难道我做错了什么?这是我应该报告的错误吗?

我使用的是 HXT 版本 9.3.1.3(撰写本文时为最新版本)。 ghc --version 打印“The Glorious Glasgow Haskell Compilation System, version 7.4.1”。我还在装有 ghc 7.6.3 的盒子上进行了测试,得到了相同的结果。

XML 文件具有以下重复结构(完整文件可在 here 中找到)

<guestbook>
<guest>
<fname>John</fname>
<lname>Steinbeck</lname>
</guest>
<guest>
<fname>Henry</fname>
<lname>Ford</lname>
</guest>
<guest>
<fname>Andrew</fname>
<lname>Carnegie</lname>
</guest>
</guestbook>

最佳答案

getGuest'' 你有

... (hasName "fname") -< x
... (hasName "lname") -< x

也就是说,您限制了 x"fname" 并且 x"lname",任何 x 都不满足!

关于haskell - 将箭头从箭头中分解出来是否是有效的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com