
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@froglogic.com www.froglogic.com - Multi-Platform GUI Testing