Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:42:19+00:00 2026-05-14T15:42:19+00:00

In Scala 2.8.x, a new annotation ( @tailrec ) has been added that gives

  • 0

In Scala 2.8.x, a new annotation (@tailrec) has been added that gives a compile-time error if the compiler cannot perform a tail-call optimization on the annotated method.

Is there some similar facility in Clojure with respect to loop/recur?

EDIT:
After reading the first answer to my question (thanks, Bozhidar Batsov) and further searching in the Clojure docs, I came across this:

(recur exprs*)
Evaluates the exprs in order, then, in parallel, rebinds the bindings of the recursion point to the values of the exprs. If the recursion point was a fn method, then it rebinds the params. If the recursion point was a loop, then it rebinds the loop bindings. Execution then jumps back to the recursion point. The recur expression must match the arity of the recursion point exactly. In particular, if the recursion point was the top of a variadic fn method, there is no gathering of rest args – a single seq (or null) should be passed. recur in other than a tail position is an error.

Note that recur is the only non-stack-consuming looping construct in Clojure. There is no tail-call optimization and the use of self-calls for looping of unknown bounds is discouraged. recur is functional and its use in tail-position is verified by the compiler [emphasis is mine].

(def factorial
  (fn [n]
    (loop [cnt n acc 1]
       (if (zero? cnt)
            acc
          (recur (dec cnt) (* acc cnt))))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:42:19+00:00Added an answer on May 14, 2026 at 3:42 pm

    There is no tail-call optimization when you use loop/recur AFAIK. A quote from the official docs:

    In the absence of mutable local
    variables, looping and iteration must
    take a different form than in
    languages with built-in for or while
    constructs that are controlled by
    changing state. In functional
    languages looping and iteration are
    replaced/implemented via recursive
    function calls. Many such languages
    guarantee that function calls made in
    tail position do not consume stack
    space, and thus recursive loops
    utilize constant space. Since Clojure
    uses the Java calling conventions, it
    cannot, and does not, make the same
    tail call optimization guarantees.
    Instead, it provides the recur special
    operator, which does constant-space
    recursive looping by rebinding and
    jumping to the nearest enclosing loop
    or function frame. While not as
    general as tail-call-optimization, it
    allows most of the same elegant
    constructs, and offers the advantage
    of checking that calls to recur can
    only happen in a tail position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Scala, and ran across a small hiccup that has been
I've been looking at the new Scala 2.9 parallel collections and am hoping to
I am new to Scala, and I cannot seem to find this information. Currently
I'm new to Scala, so its probable that this is an incredibly obvious mistake.
Good day, After play new todo (new scala project) -> cd todo -> play
I'm new to scala and most functional languages and I'm currently trying to factor
I'm new to Scala and don't know Java. I want to create a jar
I'm new to Scala, so I may be off base on this, I want
I am new to Scala, just started learning, so this is basic beginner question.
I'm new to Scala ( Scala code runner version 2.7.7.final ), and I really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.