Created
November 21, 2023 13:43
-
-
Save wsgac/b0602b2c84ab873858e4b1364de79794 to your computer and use it in GitHub Desktop.
Y combinator in various Lisps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun factorial-y-combinator (n) | |
(funcall | |
(Y (lambda (f n) | |
(if (zerop n) | |
1 | |
(* n (funcall f f (1- n)))))) | |
n)) | |
(defun tail-factorial-y-combinator (n) | |
(funcall | |
(Y (lambda (f n acc) | |
(if (zerop n) | |
acc | |
(funcall f f (1- n) (* n acc))))) | |
n 1)) | |
(defun Y (proc) | |
(lambda (&rest arg) | |
(apply proc proc arg))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- lexical-binding: t -*- | |
(cl-defun Y (proc) | |
(lambda (&rest arg) | |
(apply proc proc arg))) | |
(cl-defun factorial-y-combinator (n) | |
(funcall | |
(Y (lambda (f n) | |
(if (zerop n) | |
1 | |
(* n (funcall f f (1- n)))))) | |
n)) | |
(cl-defun tail-factorial-y-combinator (n) | |
(funcall | |
(Y (lambda (f n acc) | |
(if (zerop n) | |
acc | |
(funcall f f (1- n) (* n acc))))) | |
n 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (Y proc) | |
(lambda (. arg) | |
(apply proc proc arg))) | |
(define (factorial-y-combinator n) | |
((Y (lambda (f n) | |
(if (zero? n) | |
1 | |
(* n (f f (- n 1)))))) n)) | |
(define (tail-factorial-y-combinator n acc) | |
((Y (lambda (f n acc) | |
(if (zero? n) | |
acc | |
(f f (- n 1) (* n acc))))) n acc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment