Library function to compose a function with itself n times

前端 未结 10 2022
野的像风
野的像风 2020-11-30 03:36

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

For example I have this function:

func :: a ->         


        
10条回答
  •  旧巷少年郎
    2020-11-30 04:12

    Here's a version that has complexity O(log n) instead of O(n) (to build the function, not to apply it):

    composeN 0 f = id
    composeN n f
        | even n = g
        | odd  n = f . g
        where g = g' . g'
              g' = composeN (n `div` 2) f
    

提交回复
热议问题