# I've seen the J and the C, how about the Haskell?

Submitted by metaperl on Fri, 03/30/2007 - 1:38pm.
Some swarthy Haskell'ing lad needs to step up to the plate on this one. How would Haskell do this? We simply want the column of the maximum value of the array.

C Language

```int i, j, maxcol = 0;
float maxval = x[0][0];

for(i = 0;i<=xsize0;++i) {
for(j = 0;j<=xsize1;++j) {

if(x[i][j] > maxval) {
maxval = x[i][j];
maxcol = j;
}

}
}
```
J Language
```maxcol =. (i. >./) >./ x
```

My that J code is quite concise ... so now we need to see that H language ... heheh.

I had a solution, it was ugly and I was going to clean it up, but it occurs to me that the reason I really like Haskell is that the algorithms it encourages do not require such annoying routines. I suppose that should be a measure of a languages greatness: how well it keeps you from having to jump though frivolous hoops.

well I would love to see your solution.

I assume the type is [[Int]] -> Int instead of actual O(1) arrays. I've done it two ways, one (not shown) is a 'C' like algorithm in which you travel each element in each list in order. The one shown below gets the maximum of each sub list then finds the column of the maximum element in this 'summary list'. As I said, ugly - perhaps due to it being a 5-10 minute hack in the morning, but I am also not one to find the cleanest Haskell solution using point-free (pointless?) Haskell. ``` getMCol :: [[Int]] -> Int getMCol arr = getMCol' (map maximum arr) Nothing 0 0 getMCol' [] _ maxC _ = maxC getMCol' (a:rest) Nothing maxC currC = getMCol' rest (Just a) maxC (currC + 1) getMCol' (a:rest) (Just m) maxC currC = if a > m then getMCol' rest (Just a) currC (currC + 1) else getMCol' rest (Just m) maxC (currC + 1) ```
```import Data.List (transpose)
import Control.Arrow ((>>>))

findMaxCol1 = transpose >>> map maximum >>> (`zip` [0..]) >>> maximum >>> snd

findMaxCol [] = 0
findMaxCol l = findMaxCol1 l```

If you didn't care about empty matrices, you could just use `findMaxCol1`.

And if you don't like pointless code:

```findMaxCol [] = 0
findMaxCol matrix =
let cols = transpose matrix
col_maxs = map maximum cols
numbered_cols = zip col_maxs [0..]
max_col_pair = maximum numbered_cols
in snd max_col_pair```

Here's my pointfree Haskell solution, seems to work:

maxcol = snd . maximum . ((flip zip) [0..]) . (map maximum)

Very similar to the Data.List one above, and can be translated directly into the J solution. Doesn't trap empty arrays without a little more pattern-matching.

Boy, no matter how many times I try to pick up J, it never sticks. Learning Haskell is helping though!

Here's my solution that actually uses arrays, not lists:

```type Matrix = Array (Int, Int) Double

compareBy f x y = compare (f x) (f y)

maxcol :: Matrix -> Int
maxcol = snd . fst . maximumBy (compareBy snd) . assocs

```

Algorithm is lacking the function if when 2 maxC is linked currC =
My variant
getMCol :: [[Int]] -> Int getMCol arr = getMCol' (map maximum arr) Nothing 0 0 getMCol' [] _ maxC _ = maxC getMCol' (a:rest) Nothing maxC currC = getMCol' rest (Just a) maxC (currC + 1) getMCol' (a:rest) (Just m) maxC currC = if a > m then getMCol' rest (Just a) currC (currC + 1) else getMCol' rest (Just m) maxC (currC + 1)

Evan
Programmer of fsbo listing giant

in K that would be:

`|/`