Skip to content

Instantly share code, notes, and snippets.

@rrees
Created November 25, 2012 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrees/4145023 to your computer and use it in GitHub Desktop.
Save rrees/4145023 to your computer and use it in GitHub Desktop.
Clojure tail recursion
(defn all-nums? [cs]
(let [i (first cs) r (rest cs)]
(if (and i (Character/isDigit i))
(if (empty? r) true (recur r))
false)))
(all-nums? (seq "1234")) ;; true
(all-nums? (seq "12a4")) ;; false
(all-nums? (seq "")) ;; false
(defn all-nums? [input]
(loop [cs input]
(let [i (first cs) r (rest cs)]
(if i
(if (Character/isDigit i)
(if (empty? r)true (recur r))
false)
false))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment