如何调用相同的函数“n"次? [英] How to call the same function 'n' times?

查看:18
本文介绍了如何调用相同的函数“n"次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
将函数与自身组合 n 次的库函数

我需要一个函数来调用另一个函数 n 次.

I need a function to call another function n number of times.

所以它看起来像这样f n = g(g(g(g(l))))其中 n 等于函数 g 嵌套的数量.

so it would look something like this f n = g(g(g(g(l)))) where n equals to the number of function g nested.

我该怎么办?谢谢!

推荐答案

iterate 是一个常见的解决方案:

iterate is a common solution:

> :t iterate
iterate :: (a -> a) -> a -> [a]

所以,给定一个域与其范围相同的函数,a ->a 和一个初始输入 a,以下列形式产生一个无限的结果列表:

So, given a function with a domain the same as its range, a -> a, and an initial input a, produce an infinite list of results in the form:

iterate f a --> [a, f(a), f(f(a)), ...]

您可以使用 !!:

And you can access the nth element of the list using !!:

iterate f a !! n

注意 迭代 f a !!0 == a.

这篇关于如何调用相同的函数“n"次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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