将函数与自身组合 n 次的库函数 [英] Library function to compose a function with itself n times

查看:14
本文介绍了将函数与自身组合 n 次的库函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 中是否有库函数可以将函数与自身组合 n 次?

Is there a library function available in Haskell to compose a function with itself n times?

例如我有这个功能:

func :: a -> a

我想这样做:

func . func . func . func . func . func , ... 

(最多 n 次,其中 n 仅在运行时已知).

(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.

推荐答案

iterate 解决方案很好,或者你可能喜欢这个:n 个副本的组合ffoldr (.) id (replicate nf).

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屋!

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