Haskell: Can this code be optimized? : <p>I have the following code. I want to know whether there is any way it can be optimized so that it runs faster.</p>

<p>I wanted to solve it using a <code>SegmentTree</code> but I am not well versed with Haskell, which is why I took the following list (non-tree) approach.</p>

<pre><code>-- Program execution begins here
main :: IO()
main = do
  _ &lt;- getLine
  arr &lt;- map (read :: String -&gt; Integer) . words &lt;$&gt; getLine
  queryList &lt;- readData
  let queries = map (read :: String -&gt; Integer) queryList
  putStrLn ""
  mapM_ print $ compute queries arr  

-- Construct a sublist
sublist :: Integer -&gt; Integer -&gt; [Integer] -&gt; [Integer]
sublist start end list = take (fromInteger end - fromInteger start) . drop (fromInteger start) $ list

-- Calculate the resulting list
compute :: [Integer] -&gt; [Integer] -&gt; [Integer]
compute [_] [_] = []
compute [_] [] = []
compute [_] (_:_) = []
compute [] (_:_) = []
compute [] [] = []
compute (x1:x2:xs) list = result : compute xs list where
  result = frequency $ sublist x1 x2 list 

-- Read query list, end at terminating condition
readData :: IO [String]
readData = do
  x &lt;- getLine
  if x == "0"
    then return []
    else do xs &lt;- readData
            return (words x ++ xs)

-- Return count of the most frequent element in a list
frequency :: [Integer] -&gt; Integer
frequency list = toInteger (snd $ maximumBy (compare `on` snd) counts) where
  counts = nub [(element, count) | element &lt;- list, let count = length (filter (element ==) list)]
</code></pre>

<p>Thanks.</p>
