简体   繁体   English

Haskell泛化问题(涉及列表推导)

[英]Haskell generalizing problem (involving list comprehensions)

Let's say I want to know all the points on a (x, y) plane that are in the rectangle has . 比方说,我想知道在所有点(x, y)是在矩形平面has

I can calculate that using List Comprehensions, this way: 我可以用这种方式使用List Comprehensions来计算:

let myFun2D = [(x, y) | x <- [0..2], y <- [0..2]]

Now, if I want to accomplish the same for a (x, y, z) space, I can go the same way and do: 现在,如果我想为(x, y, z)空间完成相同的操作,我可以采用相同的方式做:

let myFun3D = [(x, y, z) | x <- [0..2], y <- [0..2], z <- [0..2]]

Is there a way to generalize this for any number of dimensions? 有没有办法将此概括为任意数量的维度? If yes, how? 如果有,怎么样?

let myFunGeneralized = ?

Thanks 谢谢

Unfortunately, because [(a,a)] and [(a,a,a)] etc are of different types, you can't write one function to represent all of them. 不幸的是,因为[(a,a)][(a,a,a)]等具有不同的类型,所以不能编写一个函数来表示它们。

Anyway, in general you could use 无论如何,一般来说你可以使用

Prelude> let x = [0..2]
Prelude> import Control.Applicative 
Prelude Control.Applicative> (,,) <$> x <*> x <*> x
[(0,0,0),(0,0,1),(0,0,2),(0,1,0),(0,1,1),(0,1,2),(0,2,0),(0,2,1),(0,2,2),(1,0,0),(1,0,1),(1,0,2),(1,1,0),(1,1,1),(1,1,2),(1,2,0),(1,2,1),(1,2,2),(2,0,0),(2,0,1),(2,0,2),(2,1,0),(2,1,1),(2,1,2),(2,2,0),(2,2,1),(2,2,2)]

If you want an [[a]] instead, there is a very simple function for this: 如果你想要一个[[a]] ,有一个非常简单的功能:

Prelude> sequence (replicate 3 x)
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[0,2,0],[0,2,1],[0,2,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2],[1,2,0],[1,2,1],[1,2,2],[2,0,0],[2,0,1],[2,0,2],[2,1,0],[2,1,1],[2,1,2],[2,2,0],[2,2,1],[2,2,2]]

or (thanks sdcvvc) 或者(感谢sdcvvc)

Prelude> import Control.Monad
Prelude Control.Monad> replicateM 3 x
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[0,2,0],[0,2,1],[0,2,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2],[1,2,0],[1,2,1],[1,2,2],[2,0,0],[2,0,1],[2,0,2],[2,1,0],[2,1,1],[2,1,2],[2,2,0],[2,2,1],[2,2,2]]

The list to tuple problem could be handled with Template Haskell like this (running ghci -XTemplateHaskell ): 可以使用Template Haskell来处理元组问题的列表(运行ghci -XTemplateHaskell ):

> import Language.Haskell.TH
> let x = [0..2]
> let tt n l = listE [tupE [[|l!!i|] | i <- [0..(n-1)]] | l <- sequence $ replicate n l ]
> $(tt 2 x)
[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]
> $(tt 3 x)
[(0,0,0),(0,0,1),(0,0,2),(0,1,0),(0,1,1),(0,1,2),(0,2,0),(0,2,1),(0,2,2),(1,0,0),(1,0,1),(1,0,2),(1,1,0),(1,1,1),(1,1,2),(1,2,0),(1,2,1),(1,2,2),(2,0,0),(2,0,1),(2,0,2),(2,1,0),(2,1,1),(2,1,2),(2,2,0),(2,2,1),(2,2,2)]

You could use something like this: 你可以使用这样的东西:

myFun :: Integer -> [[Integer]] -- Param: number of dimensions
myFun dim = snd $
  until ((== 0) . fst) --recursive build your tuple
  (\(d,lst) -> (pred d,[x:l|x <- [0..2],l <- lst]))
  (dim,[[]])

This will give you a list of point lists, you can assume that all these sublists have the same length. 这将为您提供一个点列表列表,您可以假设所有这些子列表具有相同的长度。 It should work like this: 它应该像这样工作:

> myFun 0
  []
> myFun 1
  [[0],[1],[2]]
> myFun 2
  [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM