在 Haskell 中动态构建列表理解
我很好奇是否可以在 Haskell 中动态构建列表理解。
举个例子,如果我有以下内容:
all_pows (a,a') (b,b') = [ a^y * b^z | y <- take a' [0..], z <- take b' [0..] ]
我得到了我想要的东西
*Main> List.sort $ all_pows (2,3) (5,3)
[1,2,4,5,10,20,25,50,100]
但是,我真正想要的是拥有类似的东西
all_pows [(Int,Int)] -> [Integer]
这样我就可以支持 N
对参数而无需构建
个版本。我对 Haskell 还很陌生,所以我可能忽略了一些明显的事情。这可能吗?all_pows
的 N
I am curious if it is possible to dynamically build a list comprehension in Haskell.
As an example, if I have the following:
all_pows (a,a') (b,b') = [ a^y * b^z | y <- take a' [0..], z <- take b' [0..] ]
I get what I am after
*Main> List.sort $ all_pows (2,3) (5,3)
[1,2,4,5,10,20,25,50,100]
However, what I'd really like is to have something like
all_pows [(Int,Int)] -> [Integer]
So that I can support N
pairs of arguments without building N
versions of all_pows
. I'm still pretty new to Haskell so I may have overlooked something obvious. Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列表单子的魔力:
这可能值得更多解释。
您可以自己编写
这样的
cartesianProduct [[1],[2,3],[4,5,6]]
⇒[[1,2,4],[1 ,2,5],[1,2,6],[1,3,4],[1,3,5],[1,3,6]]
。但是, 推导式 和 monad 是故意相似的。标准 Prelude 具有
sequence :: Monad m => [ma]-> m [a]
,当m
是列表 monad[]
时,它实际上完全按照我们上面写的方式执行。作为另一个快捷方式,
mapM :: Monad m => (a→mb)→ [一]-> m [b]
只是sequence
和map
的组合。对于每个底数的不同幂的每个内部列表,您希望将它们乘以一个数字。您可以递归地
或使用折叠来
编写此代码,但实际上
产品 :: Num a => [一]-> a
已经定义了!我喜欢这门语言☺☺☺The magic of the list monad:
This probably deserves a bit more explanation.
You could have written your own
such that
cartesianProduct [[1],[2,3],[4,5,6]]
⇒[[1,2,4],[1,2,5],[1,2,6],[1,3,4],[1,3,5],[1,3,6]]
.However, comprehensions and monads are intentionally similar. The standard Prelude has
sequence :: Monad m => [m a] -> m [a]
, and whenm
is the list monad[]
, it actually does exactly what we wrote above.As another shortcut,
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
is simply a composition ofsequence
andmap
.For each inner list of varying powers of each base, you want to multiply them to a single number. You could write this recursively
or using a fold
but actually,
product :: Num a => [a] -> a
is already defined! I love this language ☺☺☺