5 import Data.Array.IArray
6 import Data.Array.Unboxed
10 -- Command-line interface with simple tab-separated input/output formats.
11 -- ./match <example.in
13 -- pretty silly but it does the job
14 swapTabCommaIn s = map (\c -> if c == '\t' then ',' else if c == ',' then '\t' else c) s
15 removeQuotes s = filter (\c -> not (c == '"')) s
17 -- Some versions of Text.CSV do not use or accept a trailing newline; compensate for that.
18 removeTrailingNewline s = if not (null s) && last s == '\n' then init s else s
19 addTrailingNewline s = if not (null s) && last s /= '\n' then s ++ ['\n'] else s
21 parseTSV fname str = case parseCSV fname (swapTabCommaIn $ removeTrailingNewline str) of
23 Right ll -> Right $ map (map swapTabCommaIn) ll
24 printTSV ll = addTrailingNewline $ removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll
27 incsv <- hGetContents stdin
28 -- handle errors another day, or let the platform do it
29 let Right inll = parseTSV "standard input" incsv
30 let loadList = head inll
31 let numRvrs = length loadList
32 let loadA = listArray (0, numRvrs-1) (map read loadList)
33 let numProps = length (tail inll)
34 -- explicit type on the next line appears to be necessary
35 let pxarr = listOfListsToArray2D (tail inll) :: Array (Int,Int) String
36 -- careful, we end up transposing the array in here
37 let prefA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read (pxarr ! (j, i)))
38 let theInst = PMInstance numRvrs numProps loadA prefA
39 let PMatching theMatching = doMatching pmDefaults theInst
40 hPutStr stdout $ printTSV $ map (\(i, j) -> map show [i, j]) theMatching