Skip to content

Instantly share code, notes, and snippets.

@divs1210
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divs1210/6e388a31d4e0a425efe7 to your computer and use it in GitHub Desktop.
Save divs1210/6e388a31d4e0a425efe7 to your computer and use it in GitHub Desktop.
Babbage's Difference Engine in Clojure
(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)))
@divs1210
Copy link
Author

This code is part of a blog post that I wrote sometime back.
Here's a version in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment