[Haskell-cafe] Slow IO or bad code?

Vimal j.vimal at gmail.com
Thu Aug 9 08:21:27 EDT 2007


Hi
I am practicing writing code in haskell, by solving problems at this
site. http://spoj.pl.
The problem http://spoj.pl/problems/FASHION , is pretty simple.

1. Given two lists A,B , of N numbers, sort them and take sum of products.
    i.e. Sum ai * bi

I wrote a code, but seems to give "Time limit exceeded"!

>> Beginning of CODE
loop t function
  | t == 1 = do function
  | otherwise = do { function; loop (t - 1) function }

prod [] [] = 0
prod (a:as) (b:bs) = a*b + prod as bs

to_int :: [String] -> [Integer]
to_int [] = []
to_int (x:xs) = (read x) : to_int xs

doit = do
  n <- getLine
  a <- getLine
  b <- getLine
  let la = to_int (words a);
      lb = to_int (words b); in
    print (prod la lb)

main = do
  t <- getLine
  loop (read t) doit
<< END OF CODE

I would love to see if there is any improvement that can be done, to
the code ...

Thanks!

Vimal


More information about the Haskell-Cafe mailing list