库函数自己编写一个函数n次 [英] Library function to compose a function with itself n times
本文介绍了库函数自己编写一个函数n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Haskell中是否有一个库函数可以用它自己组成一个函数?
例如我有这个函数:
func :: a - > a
我想这样做:
func。 func。 func。 func。 func。 func,...
(高达 n 次,其中 n 仅在运行时才知道)。
请注意,迭代函数并不适合我正在做的事情,因为我不在乎任何中间结果。
解决方案
n
f
的副本的组成是 foldr(。)id(复制nf)
。
Is there a library function available in Haskell to compose a function with itself n times?
For example I have this function:
func :: a -> a
and I want to do this:
func . func . func . func . func . func , ...
(up to n times, where n is only known at runtime).
Note that the iterate function would not be appropriate for what I am doing, since I do not care about any intermediate results.
解决方案
The iterate
solution is fine, or you might like this one: the composition of n
copies of f
is foldr (.) id (replicate n f)
.
这篇关于库函数自己编写一个函数n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文