如何在Haskell中减少多个eta [英] How to make multiple eta reductions in Haskell

查看:128
本文介绍了如何在Haskell中减少多个eta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务可以从 [[a]] 矩阵中获得一列。

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)

我觉得我可以轻易摆脱 n ,但我无法做到这一点。

I sense that I could get rid of n easily, but I can't do it.

推荐答案


$ b $

What you're looking for is

colFields = map . flip (!!)

然而,这不是很清楚,我会离开<那里有code> n 参数。用 n 作为显式参数,我立即明白了函数的作用。没有它,为了理解定义,我必须考虑一下,即使是像这样的简单情况。

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.

我非常简单地通过使用<一个href =http://hackage.haskell.org/package/pointfree> pointfree 工具,虽然有手动派生的方法。

I obtained this answer very simply by using the pointfree tool, although there are methods for deriving this by hand.

这篇关于如何在Haskell中减少多个eta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆