Last active
August 29, 2015 14:07
-
-
Save divs1210/6e388a31d4e0a425efe7 to your computer and use it in GitHub Desktop.
Babbage's Difference Engine in Clojure
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
(defn diffs | |
"(diffs [1 4 9]) => (3 5)" | |
[alist] | |
(map - (rest alist) alist)) | |
(defn infer | |
"(infer [1 4 9]) => (1 4 9 16 25 ...)" | |
[pattern] | |
(->> (take (count pattern) (iterate diffs pattern)) | |
(map last) reverse | |
(drop-while zero?) | |
(iterate #(reductions + %)) | |
(map last) rest | |
(concat pattern))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is part of a blog post that I wrote sometime back.
Here's a version in Python.