[Haskell-beginners] Help with improving a program

Frerich Raabe raabe at froglogic.com
Thu Jul 10 08:52:15 UTC 2014


On 2014-07-10 04:02, Marcelo Lacerda wrote:
> Hi I'm just starting with haskell and want some help with it.
>
> I tried to solve the Store Credit[1] problem from google code jam just
> to practice, the result was a slow code[2] that I find hard to read.
>
> Can you guys give me some directions on how to improve it?

Accessing list elements via (!!) is rather inefficent for larger lists (it's 
an O(n) operation), so try to avoid that. Also, the 'comb' function won't 
scale very well for your particular use case. Since you only want to get all 
pairs, something like

pairs :: [a] -> [(a, a)]
pairs [] = []
pairs [x] = []
pairs (x:xs) = map (\e -> (x, e)) xs ++ pairs xs

...would do, performing a lot better than 'comb 2'. The repeated usage of 
(++) isn't exactly efficient either though but I couldn't think of a way to 
avoid that while writing this mail. :-)

-- 
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing


More information about the Beginners mailing list