emacs.d/clones/lisp/www.cliki.net/tail recursion.html

81 lines
5.5 KiB
HTML
Raw Normal View History

2022-10-07 15:47:14 +02:00
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CLiki: Tail Recursion</title>
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
href="https://www.cliki.net/site/feed/article.atom?title=Tail%20Recursion">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/css/colorize.css">
</head>
<body>
<span class="hidden">CLiki - Tail Recursion</span>
<div id="content"><div id="content-area"><div id="article-title">Tail Recursion</div><div id="article">"Proper" Tail Recursion as found in Scheme is a feature that some miss in CL. It means carefully written recursive function calls can execute in constant space.<p>Most high-performance CL compilers can already do significant tail call elimination (see their respective manuals). In a <a href="http://groups.google.ie/groups?dq=&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;group=comp.lang.lisp&amp;selm=cavzb.3175%24nm6.18005%40news.indigo.ie"> thread</a>
on c.l.l., a vague idea of pseudostandardising across implementations was raised.
To do this, some stuff would need to be defined.<p>What would constitute a "tail context" in CL? <p>It's all very well saying do tail "elimination where possible", but you'd
need a list of where, like in Scheme R5RS section 3.5, which defines where
tail contexts are in terms of a subset of rules of the scheme grammar. The
Scheme ones are pretty common-sense I guess, but in CL may not be just
"intuitively obvious" to everyone. Or maybe they would. The
CMUCL manual section 5.5 about tail recursion in CMUCL talks about "tail
recursive positions" - but AFAICS it does not exhaustively define what all
the "tail recursive positions" / "tail contexts" might be in CL. <p>The CMUCL user manual explains in what situations tail-call elimination has
to be suppressed for other features' sake, at least in CMUCL. Various
dynamic binding issues mainly. These caveats are almost certainly not
"intuitively obvious" to someone freshly arriving in CL from Scheme.<p>The major concern would be that an implementation with a feature of
:tail-whatever and providing a known declaration of (optimise
(tail-whatever 3)) to turn it on, would at minimum comply with a
particular list of valid tail contexts, provided the programmer bore in
mind the list of known caveats. <p>So you could just #- and bomb out if you knew you were writing code that
pretty much depended on it - if a fellow with your source wanted to just
risk it on a non-compliant implementation, they'd have to make a conscious
decision to overrule your #-<p><hr>
So, we need:<p>
<b>A specification of tail contexts for all forms in CL</b><p>TODO :-)<p><b>A list of caveats</b><p>See e.g. CMUCL User Manual, 5.5<p>
<b>A *features* entry</b><p>:x-tail-rec or whatever.<p>
<b>A definition of a new declaration</b><p>e.g. from Joe Marshall in c.l.l. thread.<p>(declaim (optimize (dynamic-space 3)))
= Tail call elimination where possible<p>(declaim (optimize (dynamic-space 2)))
= Tail call elimination (when possible) for self-calls and locally
defined (i.e. FLET or LABELS) functions only, no elimination on
non-self calls.<p>(declaim (optimize (dynamic-space 1)))
= Tail call elimination (when possible) for self-calls only.<p>(declaim (optimize (dynamic-space 0)))
= No tail call elimination anywhere.<p><hr>
<a href="programming&#32;tips.html" class="category">programming tips</a></div></div>
<div id="footer" class="buttonbar"><ul><li><a href="Tail&#32;Recursion.html">Current version</a></li>
<li><a href="https://www.cliki.net/site/history?article=Tail%20Recursion">History</a></li>
<li><a href="https://www.cliki.net/site/backlinks?article=Tail%20Recursion">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=Tail%20Recursion&amp;from-revision=3825004560">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
</div>
<div id="header-buttons" class="buttonbar">
<ul>
<li><a href="https://www.cliki.net/">Home</a></li>
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
<li><a href="CLiki.html">About</a></li>
<li><a href="Text&#32;Formatting.html">Text Formatting</a></li>
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
</ul>
<div id="search">
<form action="https://www.cliki.net/site/search">
<label for="search_query" class="hidden">Search CLiki</label>
<input type="text" name="query" id="search_query" value="" />
<input type="submit" value="search" />
</form>
</div>
</div>
<div id="pageheader">
<div id="header">
<span id="logo">CLiki</span>
<span id="slogan">the common lisp wiki</span>
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
<label for="login_name" class="hidden">Account name</label>
<input type="text" name="name" id="login_name" class="login_input" />
<label for= "login_password" class="hidden">Password</label>
<input type="password" name="password" id="login_password" class="login_input" />
<input type="submit" name="login" value="login" id="login_submit" /><br />
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
</form>
</div>
</div>
</div>
</body></html>