Haskell未知大小列表理解
我必须生成满足某些要求的所有可能的数字列表。 我知道如何为固定尺寸的链条解决这个问题。 伪代码:Haskell未知大小列表理解,haskell,Haskell,我必须生成满足某些要求的所有可能的数字列表。 我知道如何为固定尺寸的链条解决这个问题。 伪代码: [ [x1,x2,x3]| x1<-[1..5],x2<-[20..30], x2 `basedOn` x1, x3<-[100..110], x3 `basedOn` x1+x2] [[x1,x2,x3]| x1在列表理解本身内部使用递归: import Data.Maybe basedOn :: Int -> Int -> Bool basedOn a b =
[ [x1,x2,x3]| x1<-[1..5],x2<-[20..30], x2 `basedOn` x1, x3<-[100..110], x3 `basedOn` x1+x2]
[[x1,x2,x3]| x1在列表理解本身内部使用递归:
import Data.Maybe
basedOn :: Int -> Int -> Bool
basedOn a b = even (a+b)
func :: [[Int]] -> [[Int]]
func a = myFun a Nothing where
myFun :: [[Int]] -> Maybe Int -> [[Int]]
myFun [] _ = [[]]
myFun (xs:xss) j = [[a] ++ b | a <- xs, b <- myFun xss (next j a), pred j a]
where
pred Nothing _ = True
pred (Just x) a = a `basedOn` x
next Nothing a = Just a
next (Just x) a = Just (x+a)
main = putStr $ show $ func [[1..5] , [6..10], [11..15]]
导入数据。可能吧
basedOn::Int->Int->Bool
基于a b=偶数(a+b)
func::[[Int]]->[[Int]]
func a=myFun a无任何
myFun::[[Int]]->可能是Int->[[Int]]
myFun[].[[]]
myFun(xs:xss)j=[[a]++b|a,借助递归。你可能会看到或者看起来像是我需要的。我是否很清楚j是累加器,它向baseOn集合添加更多元素。pred是过滤函数,它在集合为空或之后基于baseOn过滤器时添加一个。