简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM