How to make several eta abbreviations in Haskell

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

A simple solution would be

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

and when reduced by one level of abstraction, it will be

 colFields n = map (!! n) 

I feel that I can easily get rid of n , but I cannot do it.

+6
source share
1 answer

What you are looking for is

 colFields = map . flip (!!) 

However, it is not very clear to read, I would leave the parameter n . With n as an explicit parameter, I immediately understand what the function does. Without this, I should take a moment to understand this definition even for a simple case like this.

I got this answer very simply using the pointfree tool, although there are methods for getting it manually.

+13
source

Source: https://habr.com/ru/post/977160/


All Articles