ceth
ceth

Reputation: 45295

Specific maximum function

Here is my code:

data Polindrom = Polindrom { a :: Integer,
                             b :: Integer,
                             c :: Integer } deriving (Show)


euler_4 = [ p | y <- [10..20],
                z <- [10..20],
                let x = y * z,
                let p = Polindrom { a = y, b = z, c = x },   
                let s = show x,            
                s == reverse s]

Now I need to find the element of euler_4 which have max 'c'. In Ruby I have used such construction:

}.sort { |a, b|
  b[2] <=> a[2]
}[0]

How can I get it in Haskell?

Upvotes: 1

Views: 150

Answers (3)

danlei
danlei

Reputation: 14291

A third possibility is Data.Ord.comparing:

maximumBy (comparing c)

Upvotes: 1

augustss
augustss

Reputation: 23014

import Data.Function

maximumBy (compare `on` c)

Upvotes: 6

Fred Foo
Fred Foo

Reputation: 363547

maximumBy (\x y -> compare (c x) (c y))

Upvotes: 2

Related Questions