[Haskell-cafe] Efficiency/Evaluation Question

Tommy Thorn tt1729 at yahoo.com
Sun Jun 16 02:39:18 CEST 2013


I expect you'll get many replies...

> row (Grid s l) n = if (n >= s) then [] else l !! n
> 
> col g@(Grid s l) n = if (n >= s) then [] else col_ g n 0
>    where col_ (Grid s l) n i = if (i >= s) then [] else (head l !! n) :
> col_ (Grid s (tail l)) n (i + 1)

While such low-level approach (focus on the element) can certainly
be made to work, but Haskell encourages you to think in higher level
constructs.

I haven't seen this problem before but I would map the original array
from [[Int]] -> [(Int, (Int, Int), Int, Int)], that is: a list of tuples consisting
of the original element, its coordinate, the row maximum and the column
minimum. From there its a trivial filter to find your results. (I'm sure there's
a more elegant solution).

> My question: With the way Haskell works (thunks, lazy evaluation, and
> all that mystery), is it actually worth the trouble of /precalculating/
> the maximum row values and minimum column values, to compare cell values
> against? Or will, for example, something like (smallest_list_value (col
> array 1)) definitely only evaluate once?

There's not enough context to answer the specific question,
but lazy evaluation isn't magic and the answer is probably "no".

Tommy




More information about the Haskell-Cafe mailing list