Created
November 5, 2017 20:06
-
-
Save zafercavdar/72e11f5354935627b261f82f1e7df079 to your computer and use it in GitHub Desktop.
Scheme implementation of natural numbers using BigNum representation
This file contains hidden or 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
#lang racket | |
; Zafer Cavdar | |
; Scheme implementation of natural numbers using BigNum representation | |
(define zero | |
(lambda () | |
(cons 0 '()))) | |
(define is-zero? | |
(lambda (n) | |
(eq? (car n) car((zero))))) | |
(define successor | |
(lambda (n) | |
(let ([x (+ (car n) 1)]) | |
(if (>= x 10) | |
(if (null? (cdr n)) | |
(cons 0 (cons 1 '())) | |
(cons 0 (successor (cdr n)))) | |
(cons x (cdr n)) | |
)))) | |
(define x (zero)) | |
(define predecessor | |
(lambda (n) | |
(let ([x (- (car n) 1)]) | |
(if (< x 0) | |
(if (null? (cdr n)) | |
'error-only-positive-numbers | |
(cons 9 (predecessor (cdr n)))) | |
(cons x (cdr n)) | |
)))) | |
; Summation and substraction operations | |
(define increment | |
(lambda (n t) | |
(if (= t 0) | |
n | |
(increment (successor n) (- t 1))))) | |
(define decrement | |
(lambda (n t) | |
(if (= t 0) | |
n | |
(decrement (predecessor n) (- t 1))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment