rekursive array problem
Hi @ all! I have a big problem. i have a list of variables and now i need something like this fillArray ["a"] should be something like this: [[("a",True)],[("a",False)]] fillArray ["a","b"] [[("a",True),("b",True)],[("a",False),("b",True)],[("a",True),("b",False)], [("a",False),("b",False)]] and so on. i tried many things, and i dont know how to solve this. allthesame vars = [(alltrue [] vars)] ++ [(allfalse [] vars)] alltrue liste [] = liste alltrue liste (x:xs) = alltrue (liste ++ [(x,True)]) xs allfalse liste [] = liste allfalse liste (x:xs) = allfalse (liste ++ [(x,False)]) xs this works, but whats with the rest :) it would be very nice, if somebody could help me. thank you Andreas
Andreas Fuertig wrote:
fillArray ["a"] [[("a",True)],[("a",False)]]
fillArray ["a","b"] [[("a",True),("b",True)],[("a",False),("b",True)],[("a",True),("b",False)], [("a",False),("b",False)]]
A simple solution is: fill :: [a] -> [[(a,Bool)]] fill [] = [[]] fill (x:xs) = do v <- [True,False] ; vs <- fill xs ; return ((x,v):vs) have fun, -- -- Mirko Rahn -- Tel +49-721 608 7504 -- --- http://liinwww.ira.uka.de/~rahn/ ---
Andreas Fuertig writes:
fillArray ["a"] should be something like this: [[("a",True)],[("a",False)]]
A pretty generic solution using the "Bounded" and "Enum" type classes to calculate the list of all values for a given type would be: enumAll :: (Bounded a, Enum a) => [a] enumAll = [ minBound .. maxBound ] fillArray :: (Bounded b, Enum b) => [a] -> [(a,b)] fillArray xs = [ (x,y) | x <- xs, y <- enumAll ] In GHCi, you can use these functions like this: | *Main> enumAll :: [Bool] | [False,True] | | *Main> fillArray "abc" :: [(Char, Bool)] | [('a',False),('a',True),('b',False),('b',True),('c',False),('c',True)] Peter
participants (3)
-
Andreas Fuertig -
Mirko Rahn -
Peter Simons