Haskell 并行和广义(类似 SQL)列表理解问题
也许我误读了文档(http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/syntax-extns.html#parallel-list-compregnions),但在下面的代码中我期望列表推导式 zs 和 zs' 具有相同的值。然而,它们是不同的,如主打印两行不同的行所示:
{-# LANGUAGE ParallelListComp, TransformListComp #-}
import GHC.Exts
xs = [10,20..90]
ys = map (`mod`7) xs
zs = [(x,y) | x<-xs | y<-ys, then sortWith by y]
zs' = [(x,y) | (x,y) <- zip xs ys, then sortWith by y]
main = print zs >> print zs'
我只是误读了文档,还是有一些更糟糕的问题?我很惊讶类型系统没有捕获从中抽象出来的实际代码中的错误。
产生此输出:
*Main> main
[(10,0),(20,1),(30,2),(40,3),(50,3),(60,4),(70,5),(80,6),(90,6)]
[(70,0),(50,1),(30,2),(10,3),(80,3),(60,4),(40,5),(20,6),(90,6)]
谢谢。
Maybe I'm misreading the docs (http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/syntax-extns.html#parallel-list-comprehensions) but in the following code I'd expect the list comprehensions zs and zs' to have the same value. However, they are different, as shown by main printing two different lines:
{-# LANGUAGE ParallelListComp, TransformListComp #-}
import GHC.Exts
xs = [10,20..90]
ys = map (`mod`7) xs
zs = [(x,y) | x<-xs | y<-ys, then sortWith by y]
zs' = [(x,y) | (x,y) <- zip xs ys, then sortWith by y]
main = print zs >> print zs'
Am I simply misreading the docs, or is there some worse problem? I'm surprised the type system didn't catch the error in the actual code this was abstracted from.
This output is produced:
*Main> main
[(10,0),(20,1),(30,2),(40,3),(50,3),(60,4),(70,5),(80,6),(90,6)]
[(70,0),(50,1),(30,2),(10,3),(80,3),(60,4),(40,5),(20,6),(90,6)]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唐斯,谢谢,现在我不确定我做了什么才能产生正确的输出。是的,我的评论中的代码是一个语法错误(我无法修复它或在那里发表评论,因为我不再有浏览器 cookie,叹气)。除了使用“zip”之外,我没有看到任何方法可以解决这个问题,这有点令人失望,但是哦,好吧。 (哦,看起来我可以将编辑放入队列中,这与 mathoverflow 不同)。
Dons, thanks, now I'm not sure what I did that made the right output. Yes that code in my comment is a syntax error (and I can't fix it or comment there because I don't have the browser cookie any more, sigh). I don't see any way to fix this except with "zip", which is a bit disappointing, but oh well. (Oh, looks like I can put edits into a queue, unlike on mathoverflow).
看来 Solrize 的评论是正确的。第一个结果是因为横杠比逗号关联更松散。这记录在“并行列表理解”部分下的 dons 链接中。不知道对此能做什么...
下面的内容非常丑陋((x,y)的重复绑定),但顺便修复了 solrize 的语法错误...
理想的方法是用括号括起来
并行列表推导式的 |
部分,如下所示:Solrize's comment is correct, it seems. The first result is because the bar associates more loosely than the comma. This is documented in dons' link under the "Parallel List Comprehensions" section. Not sure what can be done about this...
The following is terribly ugly (duplicate bindings of (x,y)) but fixes solrize's syntax error by the way...
What would be ideal would be some way to parenthesize
|
sections of parallel list comprehensions, such as the following:对我来说看起来像一个错误。至少是未能清楚地传达句法翻译是什么。 solrize 提供的链接指出:
直觉会将
,然后 sortWith by y
放在最后的省略号中:这将翻译为
然而,它实际上似乎放在倒数第二个省略号位置:
翻译为
Looks like a bug to me. It is at minimum a failure to communicate clearly what the syntactic translation is. solrize's provdied link states:
Intuition would put the
, then sortWith by y
in the final ellipsis:Which would translate to
However, it actually seems to go in the second-to-last ellipsis spot:
Translating to