Skip to content

Instantly share code, notes, and snippets.

@jiamo
Created January 8, 2019 14:18
Show Gist options
  • Save jiamo/7b2b804f76b87fc7715d30ed2f04536d to your computer and use it in GitHub Desktop.
Save jiamo/7b2b804f76b87fc7715d30ed2f04536d to your computer and use it in GitHub Desktop.
Ycombinator4.rkt
#lang racket
;;(define Y
;; (lambda (f)
;; ((lambda (x) (f (x x))) (lambda (x) (f (x x)))) ))
;;(define almost-Y
;; (lambda (f)
;; ((lambda (x) (f (x x))) (lambda (x) (f (x x)))) ))
;; how to come it
;; redo it again
(define identity (lambda (x) x))
(define (part-factorial0 self n)
(if (= n 0)
1
(* n (self self (- n 1)))))
(part-factorial0 part-factorial0 5)
;; remove n as args
;; which mean (part-factorial1 part-factorial1) return a fun need a args
(define (part-factorial1 self)
(lambda (n)
(if (= n 0)
1
(* n ((self self) (- n 1))))))
((part-factorial1 part-factorial1) 5)
;; try to remove (self self)
;;(define (part-factorial2-wrong self)
;; (let ((f (self self))) ;; this way will halt so we need remove it
;; (lambda (n)
;; (if (= n 0)
;; 1
;; (* n (f (- n 1)))))))
;;((part-factorial2-wrong part-factorial2-wrong) 5)
;; so (self self) is func need one args
;; a lambda make it lazy to eval
(define (part-factorial2 self)
(let ((f (lambda (y) ((self self) y))))
(lambda (n)
(if (= n 0)
1
(* n (f (- n 1)))))))
((part-factorial2 part-factorial2) 5)
;; let expression can be converted into an equivalent
;; lambda expression using this equation:
;;
;; (let ((x <expr1>)) <expr2>)
;; ==> ((lambda (x) <expr2>) <expr1>)
;; f as args to new lambda
(define (part-factorial3 self)
((lambda (f)
(lambda (n)
(if (= n 0)
1
(* n (f (- n 1))))))
(lambda (y) ((self self) y)) ))
((part-factorial3 part-factorial3) 5)
;; to make it easy to understand we define
(define g
(lambda (f)
(lambda (n)
(if (= n 0)
1
(* n (f (- n 1)))))))
(define (part-factorial4 self)
(g (lambda (y) ((self self) y)) ))
((part-factorial4 part-factorial4) 5)
;; let remove self
(define part-factorial5 (part-factorial4 part-factorial4))
(part-factorial5 5)
;; let move part-factorial4 's define
;; into part-factorial5 to make part-factorial6
;; part-factorial4 to part-factorial4-local
(define part-factorial6
(let ((part-factorial4-local
(lambda (self)
(g (lambda (y) ((self self) y))))))
(part-factorial4-local part-factorial4-local)))
(part-factorial6 5)
;; make the name short
(define part-factorial7
(let ((f (lambda (self)
(g (lambda (y) ((self self) y))))))
(f f)))
(part-factorial7 5)
;; again
;; (let ((x <expr1>)) <expr2>)
;; ==> ((lambda (x) <expr2>) <expr1>)
(define part-factorial8
((lambda (f) (f f))
(lambda (self) (g (lambda (y) ((self self) y)) ))))
(part-factorial8 5)
;; short self to h
(define part-factorial9
((lambda (f) (f f))
(lambda (h) (g (lambda (y) ((h h) y)) ))))
(part-factorial9 5)
;; make we know the g is special one
;; let add a new lambda
(define Y
(lambda (g)
((lambda (f) (f f))
(lambda (h) (g (lambda (y) ((h h) y)))) )))
(define factorial10 (Y g))
(factorial10 5)
;; at last apply inner lambda expression to its argumen
(define Y-lazy
(lambda (g)
((lambda (h) (g (lambda (y) ((h h) y))))
(lambda (h) (g (lambda (y) ((h h) y)))))))
(define factorial11 (Y g))
(factorial11 5)
;
;;; apply a g to Y-lazy
;(Y-lazy g)
; =
;((lambda (h) (g (lambda (y) ((h h) y))))
; (lambda (h) (g (lambda (y) ((h h) y)))))
;
; h = (lambda (h) (g (lambda (y) ((h h) y))))
;
;(Y-lazy g)
; = (g
; (lambda (y)
; (((lambda (h) (g (lambda (y) ((h h) y))))
; (lambda (h) (g (lambda (y) ((h h) y))))) y)))
;;;
; (lambda (y)
; (((lambda (h) (g (lambda (y) ((h h) y))))
; (lambda (h) (g (lambda (y) ((h h) y))))) y))
; (lambda a y and pass a y)
; just
; ((lambda (h) (g (lambda (y) ((h h) y))))
; (lambda (h) (g (lambda (y) ((h h) y)))))
; (mean remove the y(the args))
; = (Y-lazy g)
;
;
;
; = (g (Y-lazy g))
; mean (Y f) = (f (Y f)) mean (Y f) is (Y f) = (f (Y f)) = f ( f (Y f))
;; (Y f) is a fix point of f
;; but what meaning of fix point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment