#!/usr/bin/racket
#lang racket
(define (inc x) (+ x 1))
(define (square x) (* x x))
; Composition of one-argument functions f and g
; (f, g) -> f(g(x))
(define (compose f g)
(lambda (x) (f (g x))))
(define (repeated func times)
(if (= times 1)
func
(compose func (repeated func (- times 1)))))
(display ((repeated inc 5) 5))
(newline)
(display ((repeated square 2) 5))
(newline)
((repeated inc 5) 0)
((lambda (x)
(lambda (x) (compose inc (repeated inc 4)))) 0)
((lambda (x)
(lambda (x) (compose inc
(lambda (x) (compose inc (repeated inc 3))))) 0)
IyEvdXNyL2Jpbi9yYWNrZXQKI2xhbmcgcmFja2V0CgooZGVmaW5lIChpbmMgeCkgKCsgeCAxKSkKKGRlZmluZSAoc3F1YXJlIHgpICgqIHggeCkpCgo7IENvbXBvc2l0aW9uIG9mIG9uZS1hcmd1bWVudCBmdW5jdGlvbnMgZiBhbmQgZwo7IChmLCBnKSAtPiBmKGcoeCkpCihkZWZpbmUgKGNvbXBvc2UgZiBnKQogICAgICAgIChsYW1iZGEgKHgpIChmIChnIHgpKSkpCgooZGVmaW5lIChyZXBlYXRlZCBmdW5jIHRpbWVzKQogICAgICAgIChpZiAoPSB0aW1lcyAxKQogICAgICAgICAgICAgICAgZnVuYwogICAgICAgICAgICAgICAgKGNvbXBvc2UgZnVuYyAocmVwZWF0ZWQgZnVuYyAoLSB0aW1lcyAxKSkpKSkKCihkaXNwbGF5ICgocmVwZWF0ZWQgaW5jIDUpIDUpKQoobmV3bGluZSkKCihkaXNwbGF5ICgocmVwZWF0ZWQgc3F1YXJlIDIpIDUpKQoobmV3bGluZSkKCgooKHJlcGVhdGVkIGluYyA1KSAwKQoKKChsYW1iZGEgKHgpCiAgICAgICAgKGxhbWJkYSAoeCkgKGNvbXBvc2UgaW5jIChyZXBlYXRlZCBpbmMgNCkpKSkgMCkKCigobGFtYmRhICh4KQogICAgICAgIChsYW1iZGEgKHgpIChjb21wb3NlIGluYwogICAgICAgICAgICAgICAgICAgICAgICAobGFtYmRhICh4KSAoY29tcG9zZSBpbmMgKHJlcGVhdGVkIGluYyAzKSkpKSkgMCkKCgo=