# 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.

- metaperl's blog
- Login to post comments

well I would love to see your solution.

`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)`

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

`findMaxCol1`

.And if you don't like pointless code:

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:

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: