Haskell: How to change algorithm to work on any size of list? -
i have code:
project= [ [(a,b),(c,d),(e,f)] | a<-[1..5], b<-[1..3], c<-[1..5], d<-[1..3], e<-[1..5], f<-[1..3] , a*b + c*d + e*f <6 , + c + e == 5 , b == 3 || d==3 || f==3 ] x=take 1 project main = print $ x
it return list of 3 pairs [(x,y),(x,y),(x,y)] . there 3 conditions:
- if sum x must 5.
- if sum
x*y
less 6. - there @ least 1
y
equal 3.
now, want same algorithm work longer list example 10 pairs. how should that?
here:
project n = [ x | x <- replicatem n $ lifta2 (,) [1..5] [1..3] , sum (map (uncurry (*)) x) < 6 , sum (map fst x) == 5 , ((==3) . snd) x ] main = print $ take 1 $ project 3
or so:
project n = filter (any ((==3) . snd)) $ filter ((==5) . sum . map fst) $ filter ((<6) . sum . map (uncurry (*))) $ replicatem n $ lifta2 (,) [1..5] [1..3]
Comments
Post a Comment