And the reason for this is to be able to simulate tail-call optimization even though the Java VM doesn’t natively support that; recur can allow you to express things in a style that looks recursive, but does not suffer from stack space limitations that actual recursive calls do. If you actually want to perform a recursive call, you can simply do so by calling the function normally, without using recur.
On Monday, April 18, 2016 at 3:47:10 PM UTC-5, adrian...@mail.yu.edu wrote: > > Each arity defined for a particular Clojure function is actually a > separate "method" under the hood for that particular functional instance. > `recur` in Clojure should be thought of as more like a primitive looping > construct than a recursive call to some named function. > > The documentation for the recur special form explains this in more detail: > http://clojure.org/reference/special_forms#recur. > > On Monday, April 18, 2016 at 4:16:30 PM UTC-4, andmed wrote: >> >> Hello >> >> I am just a beginner Clojure programmer and I am unhappy with the way >> recur behaves in Clojure >> >> I have implemented kata from here >> https://github.com/alvarogarcia7/kata-formulation-find-comments/pull/3 >> What it does: it outputs 'comments' (sequences starting with "#" or "//") >> and ignores 'comments' in quotes >> >> Here is the code: >> >> (def comment-s '("#" "//")) >> (def quote-s '("'" "\"")) >> >> (defn comment-s? [s] >> (filter #(.startsWith s %) comment-s)) >> >> (defn quote-s? [s] >> (filter #(.startsWith s %) quote-s)) >> >> (defn find-comment-in-line >> "We implement a FSM via an overloaded function -- when inside a quote, >> pass two arguments, including opening quote symb, otherwise just one >> (string itself). Don't know yet hot to recur in an >> ([s] (when (pos? (count s)) >> (if-let [cs (seq (comment-s? s))] >> (subs s (count (first cs))) ; yes, a comment symbol found, just >> return the remainder of a string >> (if-let [qs (seq (quote-s? s))] ; no, lets check for an opening >> quote >> (find-comment-in-line (subs s 1) qs) ; yes, an opening >> quote found, now go look for an end quote >> (find-comment-in-line (subs s 1)))))) ; no, just some other >> symbol found, check for the rest >> ([s q] (when (pos? (count s)) >> (if-let [qs (seq (quote-s? s))] ; lets check if it is a quote >> (if (= qs q) ; is it a closing quote? >> (find-comment-in-line (subs s 1)) ; yes, lets check for the >> rest >> (find-comment-in-line (subs s 1) q)))))) ; no, just ignore the >> symbol, continue looking for a closing quote >> >> My idea was to make it via recur: it does not work however because recur >> apparently binds to an implementation with particular number of arguments, >> not to a function call, and I can not see any reason why it would not. >> >> With recur I was getting >> Caused by: java.lang.IllegalArgumentException: Mismatched argument count >> to recur, expected: 1 args, got: 2 >> >> >> I guess the code is a bit "imperative" and imperative programmer I am. >> Also, I can make recur work with a nullable argument and a helper function, >> still... is there any reason Clojure can not bind recur to a function call >> in accordance with the number of arguments passed? >> >> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.