使用列表作为位置参数的内容,一个多参数的函数 [英] Use contents of a list as positional arguments to a single multi-argument function

查看:151
本文介绍了使用列表作为位置参数的内容,一个多参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个标准的Haskell函数(或模式)中提取一个列表的内容和养活他们好像他们是有序的位置参数的函数?

例如,考虑功能(,)其中,给出了两个位置参数时,将使两元组从他们:

 (,)3 4  - > (3,4)

假设,而不是我的这些论点被一些外部函数调用,我无法改变,psented作为列表重新$ P $给我的 [3,4]

是否有操作的内容,这样,这将工作:

 (,)$ contents_of [3,4]

,这样就好像物品被放在源$ C ​​$ C间有空格的功能应用的contents_of 的操作行为?

例如,(,)$ contents_of [1] 应该是咖喱功能((,),1)然后需要一个多参数即可完成创建元组。

我想过是尝试给函数在列表折,用折叠功能前pressing柯里:

  foldr相似(\\ x和y  - > Y X)(,)[3,4]

但看着的类型签名 foldr相似

  foldr相似::(一 - > b  - > B) - > b  - > [一个]  -  GT; b

使得这似乎很难。 B 这里将需要的功能类型本身,但随后通过它已被应用到的参数的时间也不会与同类型的签名的函数 b 不再,导致折输入的问题。

这是在精神上Python的类似 * ARGS 结构。

我不关心严格性,这可能暗示 - 这样的事情只是是否有可能在标准哈斯克尔


解决方案

有可能重新present N元的功能,像这样:

 数据FunN R A = FunN INT(A  - > FunN R A)| FNilř

然后转换普通函数为 FunN

  f2FunN ::(FunN(A-> B)一) -  GT; FunN B A
f2FunN(FNil G)= 1 FunN(FNil。G)
f2FunN(FunN体中)= FunN(N + 1)(f2FunN。G)

然后申请一个参数列表:

  A :: FunN B A  - > [一个]  -  GT; b
一个(FNil r)的[] = R
一个(FunN _ F)(X:T)=一(F x)的牛逼
一_ _ =错误错误的元数

例如:

  prelude>一个(f2FunN $ f2FunN $ FNil(+))[1,2]
3
prelude>一个(f2FunN $ FNil(+))[1] 2
3
prelude>一个(f2FunN $ f2FunN $ FNil(+))[1,2,3]
***例外:错误元数
prelude>一个(f2FunN $ f2FunN $ FNil(+))[1]
***例外:错误元数

但是,当然,你需要知道在编译时功能的元数 - 让你知道有多少次,你可以用 f2FunN 包装功能。

Is there a standard Haskell function (or pattern) to extract the contents of a list and feed them as though they are the ordered positional arguments to a function?

For example, consider the function (,) which, when given two positional arguments, will make a two-tuple from them:

(,) 3 4 --> (3,4)

Suppose instead I have these arguments given to me by some external function call that I cannot change, represented as a list [3, 4].

Is there a "contents of" operation, such that this would work:

(,) $ contents_of [3, 4]

so that the action of contents_of behaves just as though the items had been placed in source code with spaces between them as function application?

For example, (,) $ contents_of [1] should be the curried function ((,) 1) which then takes one more argument to complete creating the tuple.

One thought I had was to try to fold the function over the list, with the fold function expressing currying:

foldr (\x y -> y x) (,) [3, 4]

but looking at the type signature of foldr:

foldr :: (a -> b -> b) -> b -> [a] -> b

makes this seem difficult. b here would need to be the function type itself, but then by the time it has been applied to the arguments it won't be a function with the same type signature as b any longer, leading to type issues in the fold.

This is similar in spirit to the Python *args construct.

I'm not concerned with the strictness properties this might imply -- just whether something like this is possible in standard Haskell.

解决方案

It is possible to represent N-ary functions like so:

data FunN r a = FunN Int (a -> FunN r a) | FNil r

Then convert plain functions into FunN:

f2FunN :: (FunN (a->b) a) -> FunN b a
f2FunN (FNil g)   = FunN 1 (FNil . g)
f2FunN (FunN n g) = FunN (n+1) (f2FunN . g)

Then apply a list of arguments:

a :: FunN b a -> [a] -> b
a (FNil r)   []    = r
a (FunN _ f) (x:t) = a (f x) t
a _          _     = error "wrong arity"

For example:

Prelude> a (f2FunN $ f2FunN $ FNil (+)) [1,2]
3
Prelude> a (f2FunN $ FNil (+)) [1] 2
3
Prelude> a (f2FunN $ f2FunN $ FNil (+)) [1,2,3]
*** Exception: wrong arity
Prelude> a (f2FunN $ f2FunN $ FNil (+)) [1]
*** Exception: wrong arity

But of course you need to know the arity of the function at compile time - so that you know how many times you can wrap the function with f2FunN.

这篇关于使用列表作为位置参数的内容,一个多参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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