aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Serrecchia2025-01-31 15:00:43 -0800
committerGitHub2025-01-31 15:00:43 -0800
commitf03726dfd3c74ab356d9d26100d5b18cc8c052da (patch)
tree150f9499921155d996f612c44435fc0d3108ba4f
parent51a886ce7935134e8d9732843447591844a9b930 (diff)
parente1a401631e6f01514d2211250907b573e425f123 (diff)
Merge pull request #2 from Salanoid/main
Thank you! Looks good!
-rw-r--r--languages/r/ruby/README.md3
-rw-r--r--languages/r/ruby/lambda-core.rb63
2 files changed, 66 insertions, 0 deletions
diff --git a/languages/r/ruby/README.md b/languages/r/ruby/README.md
new file mode 100644
index 0000000..4b26d8a
--- /dev/null
+++ b/languages/r/ruby/README.md
@@ -0,0 +1,3 @@
+Download Ruby version 3 or above
+
+Run by lambda-core.rb like this: `ruby lambda-core.rb` \ No newline at end of file
diff --git a/languages/r/ruby/lambda-core.rb b/languages/r/ruby/lambda-core.rb
new file mode 100644
index 0000000..93dfbc3
--- /dev/null
+++ b/languages/r/ruby/lambda-core.rb
@@ -0,0 +1,63 @@
+# LOGIC
+_true = ->(x) { ->(y) { x } }
+_false = ->(x) { ->(y) { y } }
+_not = ->(b) { b[_false][_true] }
+_and = ->(b1) { ->(b2) { b1[b2][_false] } }
+_or = ->(b1) { ->(b2) { b1[_true][b2] } }
+
+# CHURCH NUMERALS
+_zero = ->(f) { ->(x) { x } }
+_succ = ->(n) { ->(f) { ->(x) { f[n[f][x]] } } }
+_pred = ->(n) {
+ ->(f) {
+ ->(x) {
+ n[->(g) { ->(h) { h[g[f]] } }][->(u) { x }][->(a) { a }]
+ }
+ }
+}
+_one = _succ[_zero]
+
+# HELPERS - not pure lambda calculus
+def read_bool(b)
+ puts b["t"]["f"]
+end
+
+def read_church(n)
+ puts n[->(x) { x + 1 }][0]
+end
+
+# EXAMPLES
+puts "LOGIC"
+puts "---------------"
+puts "TRUE/FALSE"
+read_bool(_true) # t
+read_bool(_false) # f
+
+puts "NOT"
+read_bool(_not[_true]) # f
+read_bool(_not[_false]) # t
+
+puts "AND"
+read_bool(_and[_true][_true]) # t
+read_bool(_and[_true][_false]) # f
+read_bool(_and[_false][_true]) # f
+read_bool(_and[_false][_false]) # f
+
+puts "OR"
+read_bool(_or[_true][_true]) # t
+read_bool(_or[_true][_false]) # t
+read_bool(_or[_false][_true]) # t
+read_bool(_or[_false][_false]) # f
+
+puts "\nCHURCH NUMERALS"
+puts "---------------"
+puts "ZERO/SUCC"
+read_church(_zero) # 0
+read_church(_one) # 1
+read_church(_succ[_one]) # 2
+read_church(_succ[_succ[_one]]) # 3
+
+puts "PRED"
+read_church(_pred[_one]) # 0
+read_church(_pred[_succ[_one]]) # 1
+read_church(_pred[_succ[_succ[_one]]]) # 2