diff options
author | nitincodery | 2025-01-31 17:24:06 +0530 |
---|---|---|
committer | nitincodery | 2025-01-31 17:24:06 +0530 |
commit | 2f3dcfd450c4e82f99f55996a991659769f18a9f (patch) | |
tree | 58c573a540b456b475ff46dfe785c5bf706a41f8 /languages | |
parent | 3293e18952cf91aee1686ad6718b4293f6d7a047 (diff) |
ADD: Y-Combinator in Clojure
Diffstat (limited to 'languages')
-rw-r--r-- | languages/c/clojure/README.md | 2 | ||||
-rw-r--r-- | languages/c/clojure/lambda-core/src/y_combinator.clj | 9 | ||||
-rw-r--r-- | languages/c/clojure/lambda-core/test/test_runner.clj | 5 | ||||
-rw-r--r-- | languages/c/clojure/lambda-core/test/y_combinator_test.clj | 14 |
4 files changed, 28 insertions, 2 deletions
diff --git a/languages/c/clojure/README.md b/languages/c/clojure/README.md index d84137b..cbeeb34 100644 --- a/languages/c/clojure/README.md +++ b/languages/c/clojure/README.md @@ -7,3 +7,5 @@ Sourced from: https://github.com/srodrigo/lambda-calculus-in-clojure Blog post by source author (Sergio Rodrigo): 1. [Booleans in Clojure](https://srodrigo.me/lambda-calculus-in-clojure-part-1/) 2. [Numerals in Clojure](https://srodrigo.me/lambda-calculus-in-clojure-part-2/) + +Blog post about [Y-Combinator in Clojure](https://blog.klipse.tech/lambda/2016/08/07/pure-y-combinator-clojure.html) by different author (Yehonathan Sharvit). diff --git a/languages/c/clojure/lambda-core/src/y_combinator.clj b/languages/c/clojure/lambda-core/src/y_combinator.clj new file mode 100644 index 0000000..c474fbf --- /dev/null +++ b/languages/c/clojure/lambda-core/src/y_combinator.clj @@ -0,0 +1,9 @@ +(ns y-combinator) + +(def Y (fn [f] + ((fn [x] + (x x)) + (fn [x] + (f (fn [y] + ((x x) y))))))) + diff --git a/languages/c/clojure/lambda-core/test/test_runner.clj b/languages/c/clojure/lambda-core/test/test_runner.clj index 038b818..a851d4c 100644 --- a/languages/c/clojure/lambda-core/test/test_runner.clj +++ b/languages/c/clojure/lambda-core/test/test_runner.clj @@ -1,7 +1,8 @@ (ns test-runner (:require [clojure.test :as t] [numerals-test] - [booleans-test])) + [booleans-test] + [y-combinator-test])) (defn -main [& args] - (t/run-tests 'numerals-test 'booleans-test)) + (t/run-tests 'numerals-test 'booleans-test 'y-combinator-test)) diff --git a/languages/c/clojure/lambda-core/test/y_combinator_test.clj b/languages/c/clojure/lambda-core/test/y_combinator_test.clj new file mode 100644 index 0000000..c5396a0 --- /dev/null +++ b/languages/c/clojure/lambda-core/test/y_combinator_test.clj @@ -0,0 +1,14 @@ +(ns y-combinator-test + (:require [y-combinator :refer :all] + [clojure.test :refer :all])) + + +(def factorial-gen (fn [func] + (fn [n] + (if (zero? n) + 1 + (* n (func (dec n))))))) + +(deftest λ-Y-Combinator + (testing "factorial" + (is (= ((Y factorial-gen) 19) 121645100408832000)))) |