
Hi, i get a .csv file with two different record formats (too bad). The first one, identified by a "A" in the second field, contains 4 fields. The second one, identified by a "B", contains 10 fields. In order to put them in lists, i use lines then break to get a list of lists. Then i want to filter this list, one for the A list, the other one for the B list. Unfortunately, the pattern for the A type contains 4 fields; when a type B list occurs (10 fields), i get a message concerning the pattern : Non-exhaustive patterns in lambda How can i separate the two record formats? Thanks, Didier

Hello
If you have a list of String for each record, use partition from
Data.List, e.g.:
import Data.List ( partition )
partitionBySize :: [[String]] -> ([[String]],[[String]])
partitionBySize = partition (\xs -> length xs < 10)
-- for demo-ing:
printShortsLongs :: ([[String]],[[String]]) -> IO ()
printShortsLongs (xs,ys) = do
putStrLn "Shorts:"
mapM_ print xs
putStrLn "Longs:"
mapM_ print ys
demo1 = printShortsLongs $ partitionBySize [
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
, ["1", "2", "3", "4"]
, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
, ["1", "2", "3", "4"]
]
On 9 February 2010 10:53,
Hi, i get a .csv file with two different record formats (too bad). The first one, identified by a "A" in the second field, contains 4 fields. The second one, identified by a "B", contains 10 fields.
In order to put them in lists, i use lines then break to get a list of lists. Then i want to filter this list, one for the A list, the other one for the B list. Unfortunately, the pattern for the A type contains 4 fields; when a type B list occurs (10 fields), i get a message concerning the pattern : Non-exhaustive patterns in lambda
How can i separate the two record formats?
Thanks, Didier _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Am Dienstag 09 Februar 2010 11:53:00 schrieb legajid@free.fr:
Hi, i get a .csv file with two different record formats (too bad). The first one, identified by a "A" in the second field,
Couldn't that at least be the first field?
contains 4 fields. The second one, identified by a "B", contains 10 fields.
In order to put them in lists, i use lines then break to get a list of lists. Then i want to filter this list, one for the A list, the other one for the B list. Unfortunately, the pattern for the A type contains 4 fields; when a type B list occurs (10 fields), i get a message concerning the pattern : Non-exhaustive patterns in lambda
How can i separate the two record formats?
data Rec = A a1 a2 a3 a4 | B b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 deriving (whatever) isA :: Rec -> Bool isA (A _ _ _ _) = True isA _ = False breakIntoFields :: String -> [[String]] breakIntoFields file = map (splitOn ',') $ lines file recordToRec :: [String] -> Rec recordToRec (a1 : "A" : a2 : a3 : : a4 []) = (A (read a1) (read a2) (read a3) (read a4)) recordToRec (b1 : "B" : ... ) = (B (read b1) ...) recordToRec _ = error "invalid record format" records :: String -> [Rec] records = map recordToRec . breakIntoFields solution = Data.List.partition isA . records
Thanks, Didier
participants (3)
-
Daniel Fischer
-
legajid@free.fr
-
Stephen Tetley