Blue
Blue

Reputation: 177

Haskell functional programming, function as parameter

I'm am training funtional Haskell and cannot usderstand what is wrong with this:

maxBy :: (a -> Int) -> a -> a -> a
maxBy measure a b = max (measure a) (measure b)

Here are examples how this maxBy function should work:

maxBy (*2)   3       5      ==>  5
maxBy length [1,2,3] [4,5]  ==>  [1,2,3]
maxBy head   [1,2,3] [4,5]  ==>  [4,5]

Error message is:

Couldn't match expected type ‘a’ with actual type ‘Int’ ‘a’ is a rigid type variable bound by the type signature for: maxBy :: forall a. (a -> Int) -> a -> a -> a

Upvotes: 2

Views: 108

Answers (1)

Alistair Wall
Alistair Wall

Reputation: 332

Your function is returning measure a or measure b when you want it to return a or b.

Upvotes: 5

Related Questions