Created
November 27, 2023 07:36
-
-
Save ceving/a9e19aad951eeb82cc618eb0f8dc224f to your computer and use it in GitHub Desktop.
Church Numerals Scheme
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-syntax numeral | |
(syntax-rules () | |
((_ i s body) | |
(lambda (i) | |
(lambda (s) | |
body))))) | |
(define zero (numeral i s s)) | |
#; | |
(define zero | |
(lambda (increment) | |
(lambda (start) | |
start))) | |
(define successor | |
(lambda (predecessor) | |
(numeral i s (i ((predecessor i) s))))) | |
#; | |
(define successor | |
(lambda (predecessor) | |
(lambda (increment) | |
(lambda (start) | |
(increment ((predecessor increment) start)))))) | |
(define one (successor zero)) | |
(define two (successor one)) | |
(define three (successor two)) | |
(define add | |
(lambda (m) | |
(lambda (n) | |
(numeral i s ((m i) ((n i) s)))))) | |
#; | |
(define add | |
(lambda (m) | |
(lambda (n) | |
(lambda (increment) | |
(lambda (start) | |
((m increment) ((n increment) start))))))) | |
(define five ((add two) three)) | |
;; Helper | |
(define (println conversion x) | |
(display (conversion x)) | |
(newline)) | |
;; Implementation for numbers | |
(define number-zero 0) | |
(define (number-inc x) (+ x 1)) | |
(define (numeral->number n) ((n number-inc) number-zero)) | |
;; Implementation for asterisks | |
(define asterisk-zero "") | |
(define (asterisk-inc x) (string-append x "*")) | |
(define (numeral->asterisk n) ((n asterisk-inc) asterisk-zero)) | |
;; Results | |
(println numeral->number five) | |
(println numeral->asterisk five) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment