是否有可能在Haskell中部分应用第n个参数? [英] Is it possible to partially apply nth parameter in Haskell?

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

问题描述

我很好奇,是否可以编写一个函数 apply_nth ,它包含一个函数,一个参数的数量和该参数的值,然后返回一个新的

我得到的感觉是,由于类型系统,这是不可能的,但我无法想出满意的答案。我也不能拿出一个工作类型签名。



如果语言更松散,我想代码可能看起来像这样。

  apply_nth f 0 x = fx 
apply_nth fnx = \a- - > apply_nth(fa)(n-1)x

有什么想法?

解决方案

你的感觉是正确的,这是不可能的。部分应用程序会更改函数的类型,并且取决于您应用的参数。但是,如果该参数仅在运行时用额外参数建立索引,编译器不知道该类型是什么,编译器必须检查所有内容。实际上,您需要的结果是依赖类型,但Haskell不是依赖类型的语言。



现在,实际上,如果您抛开几个GHC扩展并引入一些奇怪的类型系列,那么您实际上可以实现类似于这种依赖类型的东西。但说实话,我怀疑这是一个好主意。无论如何,你需要什么?如果你用8个以上的参数来操作函数,那么你可能做了一些错误的事情,而对于更简单的函数,你可以定义8个组合器,每个组合器应用一个固定的参数位置。



或者,一个类似的函数可能是合理的:

pre $ apply code $ apply_nth ::([ a]→b)→> Int - > a - > [a] - > b
apply_nth fia xs = f $ before ++ [a] ++ after
where(before,after)= splitAt i xs

与参数列表不同,值列表可以很容易地包含数百个元素,所以在这种情况下,预先应用在运行时索引的单个元素是合理的。 / p>




这不仅仅是安全防范 - 因为类型在运行时甚至不存在,所以编译器需要完成所有可能依赖于类型的条件。这就是为什么Haskell像其他几种语言一样安全和简洁的 fast 可扩展的原因。

I am curious if it is possible to write a function apply_nth that takes a function, the number of a parameter, and that parameter's value and then returns a new, partially-applied function.

The feeling I get is that this is impossible due to the type system, but I can't come up with a satisfying answer. I also can't come up with a working type signature.

If the language were more loosely-typed, I imagine the code might look like this.

apply_nth f 0 x = f x
apply_nth f n x = \a -> apply_nth (f a) (n-1) x

Any ideas?

解决方案

Your feeling is correct, this isn't possible. Partial application changes the type of a function, and in which way depends on what parameter you apply. But if that parameter is indexed only at runtime with an extra argument, the compiler doesn't know what the type will be, and the compiler must typecheck everything. Really, you would need the result to have a dependent type, but Haskell is not a dependently-typed language.

Now, actually, if you toss in a couple of GHC extensions and introduce a couple of weird type families, then you can actually achieve something similar to such a dependent type. But honestly, I doubt this is a good idea. What do you need this for anyway? If you're juggling functions with more than, say, 8 parameters, you're probably doing something wrong, and for easier functions you can just define 8 combinators, each of which applies a single, fixed argument-position.

Alternatively: a similar function that's perhaps reasonable would be

apply_nth :: ([a] -> b) -> Int -> a -> [a] -> b
apply_nth f i a xs = f $ before ++ [a] ++ after
 where (before, after) = splitAt i xs

Unlike with argument-lists, a value-list can easily be hundreds of elements long, so in this case pre-applying single elements, indexed at runtime, can make sense.


This isn't just a safety precaution – it's necessary because types don't even exist at runtime, so the compiler needs to complete prepare all conditionals that might depend on types. This is why Haskell is safe and concise and fast and extensible, like few other languages.

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

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