aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Serrecchia2025-01-30 18:50:29 -0800
committerKyle Serrecchia2025-01-30 18:50:29 -0800
commitb6b87507aef85a84ea2909c05826efd9334faf7a (patch)
treed40605133b00a2afecd3f0c2d508ebdf72d7621a
parent5b1113efdb9f9c38bce07aa46c3bb441faffb76b (diff)
added javascript
-rw-r--r--README.md2
-rw-r--r--j/javascript/README.md3
-rw-r--r--j/javascript/lambda-core.js55
3 files changed, 59 insertions, 1 deletions
diff --git a/README.md b/README.md
index ad53dbd..bd475aa 100644
--- a/README.md
+++ b/README.md
@@ -33,7 +33,7 @@ By implementing these features, you will demonstrate how your chosen language ca
We invite you to contribute by adding a folder for a programming language that is not yet present. To do so:
-1. *Create a new folder* under languages/ named after the language you are implementing (e.g., languages/haskell, languages/python, languages/go, etc.).
+1. *Create a new folder* under languages/firstletterorsymbol/ named after the language you are implementing (e.g., languages/h/haskell, languages/p/python, languages/g/go, etc.).
2. Inside this folder, create:
- A file named **lambda-core** with the appropriate file extension (e.g., lambda-core.py, lambda-core.hs, lambda-core.go).
- A *README.md* that explains:
diff --git a/j/javascript/README.md b/j/javascript/README.md
new file mode 100644
index 0000000..1b35010
--- /dev/null
+++ b/j/javascript/README.md
@@ -0,0 +1,3 @@
+Download NodeJS version 4 or above
+
+Run by lambda-core.js like this: `node lambda-core.js` \ No newline at end of file
diff --git a/j/javascript/lambda-core.js b/j/javascript/lambda-core.js
new file mode 100644
index 0000000..5b4d7c9
--- /dev/null
+++ b/j/javascript/lambda-core.js
@@ -0,0 +1,55 @@
+// LOGIC
+const _true = x => y => x;
+const _false = x => y => y;
+const _not = b => b(_false)(_true)
+const _and = b1 => b2 => b1(b2)(_false)
+const _or = b1 => b2 => b1(_true)(b2)
+
+// CHURCH NUMERALS
+const _zero = f => x => x;
+const _succ = n => f => x => f(n(f)(x))
+const _pred = n => f => x => n(g => h => h(g(f)))(u => x)(a => a)
+const _one = _succ(_zero)
+
+// HELPERS - not pure lambda calculus
+const readBool = b => console.log(b("t")("f"))
+const readChurch = n => console.log(n(x => x+1)(0))
+
+// -------------------------------------------------
+
+// EXAMPLES
+console.log("LOGIC")
+console.log("---------------")
+console.log("TRUE/FALSE")
+readBool(_true) // t
+readBool(_false) // f
+
+console.log("NOT")
+readBool(_not(_true)) // f
+readBool(_not(_false)) // t
+
+console.log("AND")
+readBool(_and(_true)(_true)) // t
+readBool(_and(_true)(_false)) // f
+readBool(_and(_false)(_true)) // f
+readBool(_and(_false)(_false)) // f
+
+console.log("OR")
+readBool(_or(_true)(_true)) // t
+readBool(_or(_true)(_false)) // t
+readBool(_or(_false)(_true)) // t
+readBool(_or(_false)(_false)) // f
+
+console.log("\nCHURCH NUMERALS")
+console.log("---------------")
+console.log("ZERO/SUCC")
+readChurch(_zero) // 0
+readChurch(_one) // 1
+readChurch(_succ(_one)) // 2
+readChurch(_succ(_succ(_one))) // 3
+
+console.log("PRED")
+readChurch(_pred(_one)) // 0
+readChurch(_pred(_succ(_one))) // 1
+readChurch(_pred(_succ(_succ(_one)))) // 2
+