Skip to content

Instantly share code, notes, and snippets.

@wsgac
Created November 21, 2023 13:43
Show Gist options
  • Save wsgac/b0602b2c84ab873858e4b1364de79794 to your computer and use it in GitHub Desktop.
Save wsgac/b0602b2c84ab873858e4b1364de79794 to your computer and use it in GitHub Desktop.
Y combinator in various Lisps
(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)))
;; -*- 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))
(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