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.

Submitted by TomMD on Mon, 04/02/2007 - 7:44am.
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.
Submitted by metaperl on Mon, 04/02/2007 - 7:47am.

well I would love to see your solution.

Submitted by TomMD on Mon, 04/02/2007 - 8:18am.
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)
Submitted by j3h on Mon, 04/02/2007 - 5:51pm.
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
Submitted by sonof on Wed, 04/04/2007 - 11:17am.

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!

Submitted by palm_mute on Thu, 04/26/2007 - 8:07am.

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

Submitted by Evan Dreamz on Mon, 05/07/2007 - 7:03am.

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

Submitted by sa on Fri, 06/22/2007 - 2:56pm.

in K that would be:

|/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.