iterated functions

In the context of mathematical functions, repeated application of a function on itself.

(defun compose (&rest functions)
  (lambda (argument)
    (reduce (lambda (result fn) (funcall fn result))
            functions
            :initial-value argument)))

(defun iterate (n f)
  (if (= n 0)
      #'(lambda (x) x) 
      (compose f (iterate (- n 1) f))))

(defun increment (x)
  (+ x 1))

(funcall (iterate 10 #'increment) 0)
10

see fixed point iteration

Tags::programming:math: