Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Tail call
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language support== * [[C++ (programming language)|C++]] {{En dash}} C and C++ both do tail-call optimization.<ref>{{cite web|url=https://stackoverflow.com/questions/34125/which-if-any-c-compilers-do-tail-recursion-optimization|title=Which, if any, C++ compilers do tail-recursion optimization?|website=stackoverflow}}</ref> * [[Clojure (programming language)|Clojure]] {{En dash}} Clojure has <code>recur</code> special form.<ref>{{cite web|url=https://clojure.org/reference/special_forms#recur|title=(recur expr*)|website=clojure.org}}</ref> * [[Common Lisp]] {{En dash}} Some implementations perform tail-call optimization during compilation if optimizing for speed * [[Elixir (programming language)|Elixir]] {{En dash}} Elixir implements tail-call optimization, <ref>{{cite web|url=http://elixir-lang.org/getting-started/recursion.html|title=Recursion|website=elixir-lang.github.com}}</ref>as do all languages currently targeting the BEAM VM. * [[Elm (programming language)|Elm]] {{En dash}} Yes<ref>{{ cite web|url=https://functional-programming-in-elm.netlify.app/recursion/tail-call-elimination.html|title=Functional Programming in Elm: Tail-Call Elimination|first=Evan|last=Czaplicki}}</ref> * [[Erlang (programming language)|Erlang]] {{En dash}} Yes * [[F Sharp (programming language)|F#]] {{En dash}} F# implements TCO by default where possible <ref>{{cite web|url=https://blogs.msdn.microsoft.com/fsharpteam/2011/07/08/tail-calls-in-f/|title=Tail Calls in F#|website=msdn|date=8 July 2011 |publisher=Microsoft}}</ref> * [[Go (programming language)|Go]] {{En dash}} No support<ref>{{cite web|url=https://github.com/golang/go/issues/22624|title=proposal: Go 2: add become statement to support tail calls|website=github.com}}</ref> * [[Haskell (programming language)|Haskell]] {{En dash}} Yes<ref>{{Cite web|url=https://wiki.haskell.org/Tail_recursion|title=Tail recursion - HaskellWiki|website=wiki.haskell.org|access-date=2019-06-08}}</ref> * [[JavaScript]] {{En dash}} [[ECMAScript]] 6.0 compliant engines should have tail calls<ref>{{cite web|url=http://bdadam.com/blog/video-douglas-crockford-about-the-new-good-parts.html|title=Worth watching: Douglas Crockford speaking about the good new parts of JavaScript in 2014|first=Adam|last=Beres-Deak|website=bdadam.com}}</ref> which is now implemented on [[Safari (browser)|Safari]]/[[WebKit]]<ref>{{cite web|url=https://www.webkit.org/blog/4054/es6-in-webkit/|title=ECMAScript 6 in WebKit| date=13 October 2015}}</ref> but rejected by V8 and SpiderMonkey * [[Kotlin (programming language)|Kotlin]] {{En dash}} Has <code>tailrec</code> modifier for functions<ref>{{cite web| url=https://kotlinlang.org/docs/reference/functions.html#tail-recursive-functions|title=Functions: infix, vararg, tailrec - Kotlin Programming Language|website=Kotlin}}</ref> * [[Lua (programming language)|Lua]] {{En dash}} Tail recursion is required by the language definition<ref>{{cite web| url=https://www.lua.org/manual/5.3/manual.html#3.4.10|title=Lua 5.3 Reference Manual|website=www.lua.org}}</ref> * [[Objective-C]] {{En dash}} Compiler optimizes tail calls when -O1 (or higher) option specified, but it is easily disturbed by calls added by [[Automatic Reference Counting]] (ARC). * [[OCaml]] {{En dash}} Yes * [[Perl (programming language)|Perl]] {{En dash}} Explicit with a variant of the "goto" statement that takes a function name: <code>goto &NAME;</code><ref>{{cite web|url=http://perldoc.perl.org/functions/goto.html|title=goto - perldoc.perl.org| website=perldoc.perl.org}}</ref> * [[PureScript]] {{En dash}} Yes * [[Python (programming language)|Python]] {{En dash}} Stock Python implementations do not perform tail-call optimization, though a third-party module is available to do this.<ref>{{cite web|url=https://github.com/baruchel/tco|title=baruchel/tco|website=GitHub|date=29 March 2022}}</ref> Language inventor [[Guido van Rossum]] contended that [[stack traces]] are altered by tail-call elimination making debugging harder, and preferred that programmers use explicit [[iteration]] instead.<ref>{{cite web|url=http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html|title=Neopythonic: Tail Recursion Elimination|first=Guido Van|last=Rossum|date=22 April 2009}}</ref> In Python 3.14, a new interpreter was introduced that uses tail-call based dispatch of Python opcodes.<ref name="Python Tail-Call Interpreter Dev Discussion">{{cite web |date=2024-01-08 |title=Tail-calling interpreter |url=https://github.com/faster-cpython/ideas/issues/642 |access-date=2025-03-08 |website=GitHub}}</ref> This resulted in overall improved performance when compared to Python 3.13.<ref>{{Cite web |title=What's new in Python 3.14 |url=https://docs.python.org/3.14/whatsnew/3.14.html#a-new-type-of-interpreter |access-date=2025-02-19 |website=Python documentation |language=en}}</ref><ref name="Python Tail-Calling Interpreter Proposal">{{cite web |date=2025-01-06 |title=A new tail-calling interpreter for significantly better interpreter performance |url=https://github.com/python/cpython/issues/128563 |access-date=2025-03-08 |website=GitHub}}</ref> * [[R (programming language)|R]] {{En dash}} Yes, {{Code|tailcall()}} function introduced in R.4.4.0<ref>{{Cite web |date=2024-04-25 |title=What's new in R 4.4.0? |url=https://www.jumpingrivers.com/blog/whats-new-r44/ |access-date=2024-04-28 |website=www.jumpingrivers.com |language=en-gb}}</ref> * [[Racket (programming language)|Racket]] {{En dash}} Yes<ref>{{cite web|url=https://docs.racket-lang.org/reference/eval-model.html#(part._.Tail_.Position)|title=The Racket Reference|website=docs.racket-lang.org}}</ref> * [[Ruby (programming language)|Ruby]] {{En dash}} Yes, but disabled by default <ref>{{cite web|url=https://docs.ruby-lang.org/en/master/RubyVM/InstructionSequence.html#method-c-compile_option-3D|title=Ruby Tail Call Optimisation}}</ref> * [[Rust (programming language)|Rust]] {{En dash}} tail-call optimization may be done in limited circumstances, but is not guaranteed<ref>{{cite web|url=https://prev.rust-lang.org/en-US/faq.html#does-rust-do-tail-call-optimization|title=Rust FAQ|website=prev.rust-lang.org}}</ref> * [[Scala (programming language)|Scala]] {{En dash}} Tail-recursive functions are automatically optimized by the compiler. Such functions can also optionally be marked with a <code>@tailrec</code> annotation, which makes it a compilation error if the function is not tail recursive<ref>{{Cite web|url=https://www.scala-lang.org/api/2.13.0/scala/annotation/tailrec.html|title=Scala Standard Library 2.13.0 - scala.annotation.tailrec|website=www.scala-lang.org|access-date=2019-06-20}}</ref> * [[Scheme (programming language)|Scheme]] {{En dash}} Required by the language definition<ref>{{cite web|url=http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-6.html#%25_sec_3.5|title=Revised^5 Report on the Algorithmic Language Scheme| website=www.schemers.org}}</ref><ref>{{cite web|url=http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-8.html#node_sec_5.11| title=Revised^6 Report on the Algorithmic Language Scheme|website=www.r6rs.org}}</ref> * [[Swift_(programming_language)|Swift]] {{En dash}} In some cases (as of 2014).<ref>{{cite web |title=Does Swift implement tail call optimization? |url=https://stackoverflow.com/questions/24023580/does-swift-implement-tail-call-optimization-and-in-mutual-recursion-case |access-date=13 March 2024 |date=2014}}</ref> * [[Tcl]] {{En dash}} Since Tcl 8.6, Tcl has a {{Code|tailcall}} command<ref>{{cite web|url=http://www.tcl.tk/man/tcl/TclCmd/tailcall.htm| title=tailcall manual page - Tcl Built-In Commands|website=www.tcl.tk}}</ref> * [[Zig (programming language)|Zig]] {{En dash}} Yes<ref>{{cite web | url=https://ziglang.org/documentation/master/#call | title=Documentation - the Zig Programming Language }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)