Skip to content

Instantly share code, notes, and snippets.

@jiamo
Created September 20, 2018 10:54
Show Gist options
  • Save jiamo/8499bb69d791d524d487755ee392323c to your computer and use it in GitHub Desktop.
Save jiamo/8499bb69d791d524d487755ee392323c to your computer and use it in GitHub Desktop.
Ycombinator2
#lang racket
;(Y f) = fixpoint-of-f
;(f fixpoint-of-f) = fixpoint-of-f
;(Y f) = fixpoint-of-f = (f fixpoint-of-f)
;(Y f) = (f (Y f))
(define (Y f) (f (Y f)))
(define Y-lambda
(lambda (f)
(f (Y-lambda f))))
;; reverse use lambda to add a arg
(define Y-Strict
(lambda (f)
(f (lambda (x) ((Y-Strict f) x)))))
(define almost-factorial-half
(lambda (f)
(lambda (n)
(if (= n 0)
1
(* n (f (- n 1)))))))
; why this can't work
(define identity (lambda (x) x))
(define factorial0 (almost-factorial-half identity))
(define factorial1
(almost-factorial-half factorial0))
(define factorial2 (almost-factorial-half factorial1))
(define factorial3 (almost-factorial-half factorial2))
(define factorial-strict (Y-Strict almost-factorial-half))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment