the Y combinator is overrated

This commit is contained in:
Zander Thannhauser 2024-11-28 19:04:12 -06:00
parent c9346f8688
commit 0c87f929f4

View file

@ -12,13 +12,13 @@
# # (fib 3)
# # (fib 4)
# # (fib 5)
#
# (define! let!
# (lambda! (a b c)
# ((lambda (list! a) c) b)))
#
# (let! x 3 (+ x 2))
#
(define! let1!
(lambda! (a b c)
((lambda (list! a) c) b)))
(let1! x 3 (+ x 2))
# (define! let2!
# (lambda! (a b c d e)
# ((lambda (list! a b) e) c d)))
@ -31,18 +31,39 @@
#
# (let3! (x y z) (1 2 3) (+ x y z 2))
(define Y
(lambda (f)
(f (lambda (x) ((Y f) x)))))
# (define Y
# (lambda (f)
# (f (lambda (x) ((Y f) x)))))
#
# (define almost-factorial
# (lambda (f)
# (lambda (n)
# (if/else (= n 0)
# 1
# (* n (f (- n 1)))))))
#
# (define factorial (Y almost-factorial))
#
# (factorial 1)
# (factorial 2)
# (factorial 3)
# (factorial 4)
(define almost-factorial
(lambda (f)
(lambda (n)
# (let1! x 3 (+ x 2))
# (define almost
# (lambda (f n)
# (if/else (= n 0)
# 1
# (* n (f f (- n 1))))))
(define factorial
(let1! almost
(lambda (f n)
(if/else (= n 0)
1
(* n (f (- n 1)))))))
(define factorial (Y almost-factorial))
(* n (f f (- n 1)))))
(lambda (x) (almost almost x))))
(factorial 1)
(factorial 2)
@ -52,6 +73,3 @@