[英]How to make multiple eta reductions in Haskell
I have a task to get a column from a [[a]]
matrix. 我有一个任务是从
[[a]]
矩阵中获取一列。
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. 我觉得我可以轻易摆脱
n
,但我不能这样做。
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. 但是,这不是很清楚,我会在那里留下
n
参数。 With the n
as an explicit parameter, I understand immediately what the function does. 使用
n
作为显式参数,我立即理解该函数的作用。 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. 我通过使用pointfree工具非常简单地获得了这个答案,尽管有方法可以手动推导出这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.