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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!