Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make multiple eta reductions in Haskell

Tags:

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.

like image 911
ditoslav Avatar asked Oct 23 '14 22:10

ditoslav


1 Answers

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.

like image 86
bheklilr Avatar answered Nov 08 '22 02:11

bheklilr