Last active
December 22, 2015 17:59
-
-
Save MitchCarroll/6510102 to your computer and use it in GitHub Desktop.
wolfram rule iterator 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
(define bool-to-binary (lambda (n) (map (lambda (x) (if (equal? x #t) 1 0)) n))) | |
(define binary-to-bool (lambda (n) (map (lambda (x) (if (= x 1) #t #f)) n))) | |
(define binary | |
(lambda (number bits) | |
(if (= bits 0) '() | |
(begin | |
(let* | |
((b (if (>= number (expt 2 (- bits 1))) 1 0)) | |
(v (if (= b 1) (- number (expt 2 (- bits 1))) number))) | |
(cons b (binary v (- bits 1)))))))) | |
(define patterns (map (lambda (n) (binary n 3)) (list 7 6 5 4 3 2 1 0))) | |
(define rule (lambda (n) (binary n 8))) | |
(define check | |
(lambda (n p r) | |
(let ((a (car n)) | |
(b (if (> (length n) 1) (car (cdr n)) 0)) | |
(c (if (> (length n) 2) (car (cdr (cdr n))) 0))) | |
(if (equal? (list a b c) (car p)) | |
(car r) | |
(check n (cdr p) (cdr r)))))) | |
(define parse | |
(lambda (n r) | |
(if (> (length n) 0) | |
(cons (check n patterns r) (parse (cdr n) r)) '()))) | |
(define run | |
(lambda (n r i) | |
(let ((d (parse n r))) | |
(display n) | |
(display #\newline) | |
(if (> i 0) | |
(run d r (- i 1)) | |
(display "done"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment