Question
In Haskell Language module Problem3 where import Data.List (groupBy, sort) import Data.Function (on) type Wire = Int type Comparator = (Wire, Wire) -- Parse the
In Haskell Language
module Problem3 where
import Data.List (groupBy, sort)
import Data.Function (on)
type Wire = Int
type Comparator = (Wire, Wire)
-- Parse the input file to get the list of comparators
parseInput :: FilePath -> IO [Comparator]
parseInput filename = do
contents <- readFile filename
let ls = lines contents
return $ map parseComparator ls
parseComparator :: String -> Comparator
parseComparator line =
let [x, y] = map read $ words line
in (x, y)
-- Convert the list of comparators to the parallel form
toParallelForm :: [Comparator] -> [[Comparator]]
toParallelForm comparators =
let sortedComparators = sort comparators
groups = groupBy ((==) `on` disjoint) sortedComparators
in map (map removeDisjoint) groups
disjoint :: Comparator -> Comparator -> Bool
disjoint (x, y) (a, b) = x /= a && x /= b && y /= a && y /= b
removeDisjoint :: Comparator -> Comparator
removeDisjoint (x, y) = (min x y, max x y)
-- Write the parallel form to the output file
writeParallelForm :: FilePath -> [[Comparator]] -> IO ()
writeParallelForm filename parallelForm = do
let lines = map formatLine parallelForm
writeFile filename $ unlines lines
formatLine :: [Comparator] -> String
formatLine comparators =
let sortedComparators = sort comparators
formattedComparators = map formatComparator sortedComparators
in unwords formattedComparators
formatComparator :: Comparator -> String
formatComparator (x, y) = show x ++ " -- " ++ show y
Here is a problem in this part
-- Convert the list of comparators to the parallel form
toParallelForm :: [Comparator] -> [[Comparator]]
toParallelForm comparators =
let sortedComparators = sort comparators
groups = groupBy ((==) `on` disjoint) sortedComparators
in map (map removeDisjoint) groups
groups = groupBy ((==) `on` disjoint) sortedComparators which ( == ) is showing red, I don't know how to fix it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started