Created
April 10, 2013 23:59
-
-
Save westerp/5359526 to your computer and use it in GitHub Desktop.
Adding with numbers in Zozotez. Playing around after looking at https://gist.github.com/2584539.git
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
;; usage: ./zozotez addition.zzt or | |
;; jitbf zozotez.bf < addition.zzt or | |
;; one anonymous function to wrap all out stuff in it | |
((\() | |
;; symbols 0 to 9 to represent digits. eg. 100 is '(1 0 0) | |
(:'d2clis '(0 1 2 3 4 5 6 7 8 9)) | |
;; auxuillary function for d2c | |
(:'d2caux | |
(\ (lis num) | |
(? (= (a lis) digit) | |
num | |
(d2caux (d lis) (c '* num))))) | |
;; auxillary for church to digit | |
(:'c2daux | |
(\ (c1 lis) | |
(? c1 | |
(c2daux (d c1) (d lis)) | |
(a lis)))) | |
;; convert church to digit | |
(:'c2d (\ (c1) (c2daux c1 d2clis))) | |
;; convert digit to church | |
(:'d2c (\ (digit) | |
(d2caux d2clis ()))) | |
;; convert number to church | |
;; '(1 0) => (() (*)) | |
(:'n2c (\ (na acc) | |
(? na | |
(n2c (d na) (c (d2c (a na)) acc)) | |
acc))) | |
;; number 9 and 10 | |
(: '9 '(* * * * * * * * *)) | |
(:'10 '(* * * * * * * * * *)) | |
;; adds d1 to d2 mod 10 | |
(:'+dd (\ (d1 d2) | |
(? d1 | |
(+dd (d d1) (c '* d2)) | |
d2))) | |
(:'+ (\ (d1 d2) | |
(c2d (+dd (d2c d1) | |
(d2c d2))))) | |
;; test it with one digit imlpementation | |
(p '|1 + 1 = | ()) | |
(p (+ '1 '1)) | |
;; print a number | |
(:'numprint (\ (lis) | |
(? lis | |
((\() | |
(p (a lis) ()) | |
(numprint (d lis)))) | |
(p '| |)))) | |
;; returns T if cnum is above 9 | |
(:'carry (\ (cnum 9) | |
(? 9 | |
(? cnum | |
(carry (d cnum) (d 9))) | |
(? cnum T)))) | |
;; modulus 10 | |
(:'mod (\ (dm acc2 cnt) | |
(? cnt | |
(? dm | |
(mod (d dm) (c '* acc2) (d cnt)) | |
acc2) | |
(mod dm () 10)))) | |
;; adds lists of church numerals with lsn first | |
(:'+c (\ (c1 c2 c3 acc) | |
(? | |
(? (= c1) | |
(? (= c2) | |
(? (= c3) | |
() | |
T) | |
T) | |
T) | |
((\ (sum) | |
(+c (d c1) (d c2) (carry sum 9) (c (c2d (mod sum () 10)) acc))) | |
(+dd (a c1) (? c3 (c '* (a c2)) (a c2)))) | |
acc))) | |
;; redefine + to add two multi digit numbers | |
;; l1 and l2 are both lists with digits. eg. 123 is '(1 2 3) | |
(:'+ (\ (l1 l2) | |
(+c (n2c l1) | |
(n2c l2)))) | |
;; test it with multiple digit implementation | |
(p '|123 + 928 = | ()) | |
(numprint (+ '(1 2 3) '(9 2 8))) | |
;; end of outer anonymous lambda fucntion | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment