如何使用内部在Haskell参数的函数? [英] How do you use a function inside arguments in haskell?
问题描述
我上要求编写应用一个函数整数函数,算出答案的一个问题。这里的问题是:
声明的类型和定义一个函数,函数(比如f)和一个整数(说n)和返回f 0的+ F 1 + F 2 + ... + F N。例如,乐趣平方米5会返回55是0 + 1 + 4 + 9 + 16 + 25(平是指广场)。
有谁知道如何做到这一点?我非常AP preciate它。
这就是所谓的高阶函数
乐趣::(INT - >智力) - > INT - > INT
有趣的F N =?
和我们只需使用˚F
像一个正常的功能,那么写 f 0的
或什么的。 ???是的哈斯克尔翻译
f 0的+ F 1 + ... + F N
至于如何做到这一点,这看起来像功课所以我就提示你要看看使用 [0到n]
得到0列表到 N
和
地图::(INT - >智力) - > [INT] - > [INT] - 限制为清楚起见
该列表中的应用一个函数到每一个项目(嘿它的另一个高阶函数)
总和:: [INT] - > INT
这在一个列表加起来所有的数字。
I am on a problem that asked to write a function that applies a function to an Integer and calculate the answer. Here is the problem:
Declare the type and define a function that takes a function (say f) and an integer (say n) and returns f 0 + f 1 + f 2 + … + f n. For example, fun sq 5 would returns 55 which is 0+1+4+9+16+25 ("sq" means "square").
Does anyone know how to do that? I would very much appreciate it.
This is called a higher order function
fun :: (Int -> Int) -> Int -> Int
fun f n = ???
And we just use f
like a normal function, writing f 0
or whatever. ??? is the haskell translation of
f 0 + f 1 + ... + f n
As for how to do this, this looks like homework so I'll just hint you to look at using [0..n]
to get a list from 0 to n
and
map :: (Int -> Int) -> [Int] -> [Int] -- restricting for clarity
which applies a function to every item in a list (hey it's another higher order function)
sum :: [Int] -> Int
Which adds up all the numbers in a list.
这篇关于如何使用内部在Haskell参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!