Created
December 12, 2011 12:46
-
-
Save amidvidy/1466985 to your computer and use it in GitHub Desktop.
peano in 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
; O is a natural number. | |
(define zero 'zero) | |
; For every number n, (succ n) is a natural number. | |
(define (succ n) | |
(lambda () n)) | |
; A few numbers | |
(define one | |
(succ zero)) | |
(define two | |
(succ one)) | |
(define three | |
(succ two)) | |
(define four | |
(succ three)) | |
; Equality | |
(define (are-equal? a b) | |
(cond ((and (eq? a zero) | |
(eq? b zero)) #t) | |
((or (eq? a zero) | |
(eq? b zero)) #f) | |
(else (are-equal? (a) (b))))) | |
; Addition | |
(define (add a b) | |
(define (add-recur a b) | |
(succ (add a (b)))) | |
(if (eq? b zero) a | |
(add-recur a b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment