ditoslav
ditoslav

Reputation: 4872

How to make multiple eta reductions in Haskell

I have a task to get a column from a [[a]] matrix.

A simple solution would be

colFields :: Int -> [[a]] -> [a]
colFields n c = map (!! n) c

and when reduced by one level of abstraction it would be

colFields n = map (!! n)

I sense that I could get rid of n easily, but I can't do it.

Upvotes: 6

Views: 369

Answers (1)

bheklilr
bheklilr

Reputation: 54068

What you're looking for is

colFields = map . flip (!!)

However, this is not very clear to read, I'd leave the n parameter in there. With the n as an explicit parameter, I understand immediately what the function does. Without it, I have to think for a minute in order to understand the definition, even for a simple case like this.

I obtained this answer very simply by using the pointfree tool, although there are methods for deriving this by hand.

Upvotes: 13

Related Questions