Created
April 13, 2015 22:19
-
-
Save 0x0dea/e2ee4343624754de7643 to your computer and use it in GitHub Desktop.
This script uses 237,267 anonymous functions (and, essentially, only those functions) to determine that 17 and 10 have four bits in common.
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
$fns = 0 | |
# Automatically curry all functions for slightly greater legibility. | |
# Keep track of how many functions end up getting created (for laughs). | |
# Get to use the fancy symbol everywhere. | |
def λ | |
$fns += 1 | |
proc.curry | |
end | |
# Convert a Ruby integer to a Church numeral. | |
def church n | |
n.times.reduce(Zero) { |n| Succ[n] } | |
end | |
# Undo the above for printing/verifying that this madness wasn't all for naught. | |
def unchurch p | |
p[-> n { n + 1 }][0] | |
end | |
Zero = λ { |f, x| x } | |
One = λ { |f, x| f[x] } | |
Succ = λ { |n, f, x| f[n[f][x]] } | |
Pred = λ { |n, f, x| n[λ { |g, h| h[g[f]] }][λ { |_| x }][λ { |u| u }] } | |
Add = λ { |m, n| n[Succ][m] } | |
Sub = λ { |m, n| n[Pred][m] } | |
True = λ { |a, b| a } | |
False = λ { |a, b| b } | |
IsZero = λ { |n| n[λ { |x| False }][True] } | |
Z = λ { |f| λ { |x| f[λ { |_| x[x][_] }] }[λ { |x| f[λ { |_| x[x][_] }] }] } | |
# Pussy out and slow the whole thing down considerably. :( | |
Even = Z[λ { |f, n| | |
IsZero[n][True][λ { |_| | |
IsZero[Pred[n]][False][ | |
f[Pred[Pred[n]]]][_] }] }] | |
# Division is also really gnarly. (Me 0 | 2 Alonzo Church) | |
Halve = Z[λ { |f, n, m| | |
IsZero[Sub[n][m]][n][ | |
λ { |_| f[Pred[n]][Succ[m]][_] }] }] | |
Common = Z[λ { |f, a, b, c, i| | |
IsZero[i][c][λ { |_| | |
Add[Even[Add[a][b]][One][Zero]][ | |
f[Halve[a][Zero]][Halve[b][Zero]][c][Pred[i]]][_] }] }] | |
A = church 17 | |
B = church 10 | |
puts "# of common bits: #{unchurch Common[A, B, Zero, church(8)]}" | |
puts "Created #$fns functions!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment