{"id":20576512,"url":"https://github.com/divs1210/xodarap","last_synced_at":"2025-10-14T08:09:15.311Z","repository":{"id":62435190,"uuid":"152332648","full_name":"divs1210/xodarap","owner":"divs1210","description":"Fearless recursion in Clojure!","archived":false,"fork":false,"pushed_at":"2020-05-20T10:08:02.000Z","size":23,"stargazers_count":74,"open_issues_count":2,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-09-25T22:19:18.968Z","etag":null,"topics":["clojure","library","recursion"],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/divs1210.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-09T23:02:17.000Z","updated_at":"2024-05-31T07:49:51.000Z","dependencies_parsed_at":"2022-11-01T21:16:19.665Z","dependency_job_id":null,"html_url":"https://github.com/divs1210/xodarap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/divs1210/xodarap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Fxodarap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Fxodarap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Fxodarap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Fxodarap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/divs1210","download_url":"https://codeload.github.com/divs1210/xodarap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divs1210%2Fxodarap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279018305,"owners_count":26086334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clojure","library","recursion"],"created_at":"2024-11-16T05:46:02.493Z","updated_at":"2025-10-14T08:09:15.272Z","avatar_url":"https://github.com/divs1210.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xodarap\n[![Clojars Project](https://img.shields.io/clojars/v/xodarap.svg)](https://clojars.org/xodarap)\n[![Build Status](https://travis-ci.com/divs1210/xodarap.svg?branch=master)](https://travis-ci.com/divs1210/xodarap)\n\nFearless recursion in Clojure!\n\n## Why\n\nThe JVM puts a hard limit on how deep our functions can recurse without blowing the stack:\n\n```clojure\n;; Ex. 1\n;; =====\n(defn unsafe-factorial [n]\n  (if (\u003c n 2)\n    1\n    (*' n (unsafe-factorial (dec n)))))\n\n(unsafe-factorial 5000)\n;; =\u003e StackOverflowError   clojure.lang.Numbers.lt (Numbers.java:3816)\n```\n\nThis is a very simple example, and the fn can be defined in a tail-recursive manner to avoid this issue,\nbut we are not always this lucky! Consider:\n\n```clojure\n;; Ex. 2\n;; =====\n(defn unsafe-ackermann [m n] \n  (cond (zero? m) (inc n)\n        (zero? n) (recur (dec m) 1)\n        :else (recur (dec m) (unsafe-ackermann m (dec n)))))\n\n(unsafe-ackermann 3 10)\n;; =\u003e StackOverflowError   clojure.lang.Numbers$LongOps.inc (Numbers.java:545)\n```\nTry converting the above to use tail recursion - possible, but not trivial.\n\n... and then there are mutually-recursive functions:\n\n```clojure\n;; Ex. 3\n;; =====\n(letfn [(is-odd? [n]\n          (if (zero? n)\n            false\n            (is-even? (dec n))))\n        (is-even? [n]\n          (if (zero? n)\n            true\n            (is-odd? (dec n))))]\n  (is-even? 10000))\n;; =\u003e StackOverflowError   clojure.lang.Numbers$LongOps.isZero (Numbers.java:443)\n```\nClojure's `recur` form doesn't help us here, so we generally end up using a [trampoline](https://clojuredocs.org/clojure.core/trampoline).\n\nIs there a general way to recurse safely (ie without blowing up the stack)\nin all these cases, and without changing the structure of our code?\n\n## Usage\n\n```clojure\n(use 'xodarap.core)\n\n;; Ex. 1\n;; =====\n(defrec factorial [n]\n  (if (\u003c n 2)\n    1\n    (*' n (rec (factorial (dec n))))))\n\n(factorial 5000)\n;; =\u003e 4228577926605543522201064200233584405390...\n```\n\nHere, we define a safe recursive factorial fn using the `defrec` form. Note that the\nrecursive call to itself is wrapped in a `rec` form.\n\n```clojure\n;; Ex. 2\n;; =====\n(defrec ackermann [m n] \n  (cond (zero? m) (inc n)\n        (zero? n) (recur (dec m) 1)\n        :else (recur (dec m) (rec (ackermann m (dec n))))))\n\n(ackermann 3 10)\n;; =\u003e 8189 (after a long pause)\n```\n\nWe can crack tougher nuts using the same method.\n\n```clojure\n;; Ex. 3\n;; =====\n(letrec [(is-odd? [n]\n           (if (zero? n)\n             false\n             (rec (is-even? (dec n)))))\n         (is-even? [n]\n           (if (zero? n)\n             true\n             (rec (is-odd? (dec n)))))]\n  (is-even? 10000))\n;; =\u003e true\n```\n\nHere we define mutually recursive functions using `letrec`.\n\n```clojure\n;; like fn\n(recfn fact [n]\n  (if (\u003c n 2)\n    1\n    (*' n (rec (fact (dec n))))))\n```\n\nSimilarly, `recfn` is an alternative to `fn`.\n\n## License\n\nCopyright © 2018 Divyansh Prakash\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivs1210%2Fxodarap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivs1210%2Fxodarap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivs1210%2Fxodarap/lists"}