Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The idea of ``squaring''

Name: Anonymous 2012-07-16 4:40

How is the idea of ``squaring'' expressed?

Name: Anonymous 2012-07-18 8:09


;;; This is all we need:
(define zero (lambda (f) (lambda (x) x)))
(define add-1 (lambda (n) (lambda (f) (lambda (x) (f ((n f) x))))))

;;; We define ``two''
(define two (lambda (f) (lambda (x) (f (f x))))) ; or (define two (add-1 (add-1 zero)))

;;; ``square'' is simply:
(define square (lambda (n) (two n)))

;;; Some shit only useful for mortals:

(define (church->int n)
  ((n add1) 0))

(define int->church
  (lambda (n)
    (if (zero? n)
        zero
        (add-1 (int->church (- n 1))))))

;;; Now let's say you want 14^2
(define fourteen (int->church 14))
(church->int (square fourteen)) ;yields 196

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List