Question: module Problem3 where import Data.List (groupBy, sort) import Data.Function (on) import Networks -- Parse the input file to get the list of comparators parseInput ::
module Problem3 where
import Data.List (groupBy, sort)
import Data.Function (on)
import Networks
-- Parse the input file to get the list of comparators
parseInput :: FilePath -> IO [Comparator1]
parseInput filename = do
contents <- readFile filename
let ls = lines contents
return $ map parseComparator ls
parseComparator :: String -> Comparator1
parseComparator line =
let [x, y] = map read $ words line
in (x, y)
-- Convert the list of comparators to the parallel form
toParallelForm :: [Comparator1] -> [[Comparator1]]
toParallelForm comparators =
let sortedComparators = sort comparators
groups = groupBy ((==) `on` disjoint (head comparators)) sortedComparators
in map (map removeDisjoint) groups
disjoint :: Comparator1 -> Comparator1 -> Bool
disjoint (x, y) (a, b) = x /= a && x /= b && y /= a && y /= b
removeDisjoint :: Comparator1 -> Comparator1
removeDisjoint (x, y) = (min x y, max x y)
-- Write the parallel form to the output file
writeParallelForm :: FilePath -> [[Comparator1]] -> IO ()
writeParallelForm filename parallelForm = do
writeFile filename $ unlines $ map formatLine parallelForm
formatLine :: [Comparator1] -> String
formatLine comparators = unwords $ map formatComparator $ sort comparators
formatComparator :: Comparator1 -> String
formatComparator (x, y) = show x ++ " -- " ++ show y
Here is module Networks
module Networks where
type Network = [(Int,Int)]
type Comparator = [(Int,Int)]
type Wire = Int
type Comparator1 = (Wire, Wire)
This is problem I have after runHaskell Main: Problem3.hs:17:7-36: Non-exhaustive patterns in [x, y]
And this is question
In a comparison network, comparisons (x, y) and (a, b) may be done in parallel when the set {x, y} is disjoint from {a, b}. In that case, the two comparisons are working on separate pairs of wires, so they could execute simultaneously. We can represent a comparator network in parallel form as a list of lists. Each element list shows comparisons that can happen in parallel. For example, a parallel form for sort1.txt is the list of lists: [[(1,2),(3,4)],[(1,3),(2,4)],[(2,3)]] The command line for this part is Parallel filename Here, filename is again the name of a file containing a comparator network, like sort1.txt. Your code should write the parallel form to a file parallel.txt, in this format (for the above example): 1 -- 2 , 3 -- 4 1 -- 3 , 2 -- 4 2 -- 3 On each line, please order the comparators by first wire number. So it would not be legal to have 3 -- 4 , 1 -- 2 because the first wire number (3) of the first comparison is greater than the first wire number (1) of the second. Please use the same spacing as above so that your output can be easily compared with expected output.
Help me to fix this problem
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
