将函数应用于任意长的参数列表 [英] Applying a function to an arbitrarily long list of arguments

查看:148
本文介绍了将函数应用于任意长的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数apply,它接受一个带有任意数量参数的函数以及一个整数列表,并返回该函数的结果(其中列表中的每个整数都是一个参数。 p>

我正在考虑类似于:

  apply ::([Int] - > Int]  - > [Int]  - > Int 
apply fx:xs = apply(fx)xs
apply f [] = f

但是我知道这是行不通的,因为类型签名是错误的 - 函数不包含int列表,它只需要一定量的int参数。

另外,当我到达基本情况时,应用的f参数实际上应该是一个整数,无论​​如何都违反了类型签名。



有人知道如何处理这类问题吗?

解决方案


我想创建一个函数apply,它带有一个任意数量的参数以及一个整数列表

为什么要这样做?也许你的参数结构应该作为一个数据结构来传递,但到目前为止,你已经限制了这个问题,以确保它不会产生一个惯用的Haskell解决方案。


I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers, and returns the result of the function (Where each integer in the list is an argument in order.

I was thinking something like:

apply :: ([Int] -> Int) -> [Int] -> Int
apply f x:xs = apply (f x) xs
apply f [] = f

But I know this won't work because the type signature is wrong - the function doesn't take a list of ints, it just takes some amount of int arguments.

Additionally, when I get to the base case the f argument to apply should actually be an integer, violating the type signature anyway.

Does anyone know how to deal with this sort of problem?

解决方案

I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers,

Why do you want to do this? Perhaps your argument structure should be passed as a data structure, but so far you've over constrained the problem to ensure it won't produce an idiomatic Haskell solution.

这篇关于将函数应用于任意长的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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