(define (Y g) (let a (lambda (f) (f f)) (a (lambda (f) (g (lambda (x) (let c (f f) (c x)))))))) ;; The above was translated from this code in the LUA distro, test dir: ;; -- traditional fixed-point operator from functional programming ;; Y = function (g) ;; local a = function (f) return f(f) end ;; return a(function (f) ;; return g(function (x) ;; local c=f(f) ;; return c(x) ;; end) ;; end) ;; end (define (F-tri f) (lambda (n) (if (eq n 0) 0 (+ n (f (sub1 n)))))) (define (F-fact f) (lambda (n) (if (eq n 0) 1 (* n (f (sub1 n)))))) ;; The above was translated from this code in the LUA distro, test dir: ;; -- factorial without recursion ;; F = function (f) ;; return function (n) ;; if n == 0 then return 1 ;; else return n*f(n-1) end ;; end ;; end (def 'triangle/Y (Y F-tri)) (def 'factorial/Y (Y F-fact)) (assert (== 0 (triangle/Y 0))) (assert (== 1 (triangle/Y 1))) (assert (== '(1 2 2) (triangle/Y '(1 2)))) (assert (== '(1 2 2 3 3 3) (triangle/Y '(1 2 3)))) (assert (== '(1 2 2 3 3 3 4 4 4 4) (triangle/Y '(1 2 3 4)))) (defun tri (n) (if (eq n 0) 0 (+ n (tri (sub1 n))))) (define (eval-say-roman func n) (let z ((eval func) n) rn (roman n) rz (roman z) (say (list func rn '==> rz)))) (assert-equal 'XV. (roman (tri 5))) (assert-equal 'XV. (roman (triangle/Y 5))) (assert-equal 'CXX. (roman (factorial/Y 5))) ;(eval-say-roman 'tri 5) ;(eval-say-roman 'triangle/Y 5) ;(eval-say-roman 'factorial/Y 5)