Haskell中具有多个参数的部分应用程序 [英] Partial application in Haskell with multiple arguments

查看:140
本文介绍了Haskell中具有多个参数的部分应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一些函数f(x1,x2,x3,..,xN),将其部分应用在几个地方通常很有用.例如,对于N = 3,我们可以定义g(x)= f(1,x,3).但是,Haskell中的标准部分应用程序无法通过这种方式工作,而只能允许我们通过修复其第一个参数来部分应用一个函数(因为所有函数实际上仅采用一个参数).有没有简单的方法可以执行以下操作:

Given some function f(x1,x2,x3,..,xN) it is often useful to apply it partially in several places. For example, for N=3 we could define g(x)=f(1,x,3). However, the standard partial application in Haskell does not work this way and only allows us to partially apply a function by fixing its first arguments (because all functions actually only take one argument). Is there any simple way to do something like this:

g = f _ 2 _
g 1 3

是否输出f 1 2 3的值? 当然我们可以做一个lambda函数

with output the value of f 1 2 3? Of course we could do a lambda-function

g=(\x1 x3 -> f x1 2 x3)

但是我发现这非常难以理解.例如,在Mathematica中,它的工作原理如下:

but I find this quite unreadable. For example, in Mathematica it works like this, which I find quite nice:

g=f[#1,2,#2]&
g[1,3]

,输出为f[1,2,3].

也许我应该多说一些动机.我想在点样式合成中使用这种部分应用的函数,即在这样的表达式中使用:

Maybe I should say somehting more about the motivation. I would like to use such partially applied functions in point-style compositions, i.e., in expressions like this:

h = g. f _ 2 . k

获得h 3 = g(f(k(3),2)).

推荐答案

您可以阅读

You could read this question on how to change the order of arguments, then use partial application, but really the cleanest and clearest way of doing that currently in Haskell is just directly:

g x y = f x 2 y

这篇关于Haskell中具有多个参数的部分应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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