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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With