

GitHub - Wilfred/tco.el: Tail call optimisation in Emacs lisp
source link: https://github.com/Wilfred/tco.el
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
tco.el
Tail call optimisation for Emacs lisp
tco.el provides tail-call optimisation for functions in elisp that call themselves in tail-position. Mutually recursive functions are unchanged.
It works by replacing each self-call with a thunk, and wrapping the
function body in a loop that repeatedly evaluates the thunk. Roughly
speaking, a function foo
:
(defun-tco foo (...) (...) (foo (...)))
Is rewritten as follows:
(defun foo (...) (flet (foo-thunk (...) (...) (lambda () (foo-thunk (...)))) (let ((result (apply foo-thunk (...)))) (while (functionp result) (setq result (funcall result))) result)))
Example
;; -*- lexical-binding: t -*- (require 'tco) (setq lexical-binding 't) (defun-tco sum (n &optional accum) (setq accum (or accum 0)) (if (zerop n) accum (sum (1- n) (+ accum n)))) ;; Without TCO, values greater than `max-lisp-eval-depth' (usually ;; 600) would cause stack overflow here: (sum 700)
Known issues
Due to a bug in cl-letf in Emacs 24.3.1, this package will not work on Emacs 24.3.1.
Other Projects
recur also offers TCO for self-tail recursion.
Recommend
-
173
Rust Emacs This project isn't maintained anymore. If you are looking for a rust based emacs fork, you...
-
99
Imagine that you hold Control and type your name into Emacs. Can you describe what will happen? – The ‘Emacs Guru Test’ Emacs shortcuts (known as ‘key bindings’) can seem ridiculous to beginners. Some...
-
9
Elixir Alchemy Iteration, Recursion, and Tail-call Optimization in Elixir Jeff Kreeftmeijer on Mar 19, 2019 “I absolutely love AppSignal.”
-
13
pair-tree.el A cons cell explorer. M-x pair-tree is a learning tool for visualizing Emacs Lisp lists. If you're baffled by cons and confused by cdaadr you might want to give it a s...
-
13
ov.el Simple way to manipulate overlay for Emacs. Overlay is capable of manipulating text appearance, cursor behavior, etc. It doesn't affect font-lock or text-properties. Command You can always do M-x...
-
11
pyimport Manage Python imports from Emacs! Installation: install from MELPA, or add pyimport.el to your load-path. Unused Imports Requires pyflakes to be installed. Run M-x py...
-
4
Pattern Matching and Tail Recursion in Emacs Lisp Nov 19, 2022 • Erik L. Arneson Functional programming offers a bunch of really cool programming patterns. Two tha...
-
10
tail-call-proxy Delayed initialized objects that support tail-call optimization. Functions ▸ lazy<T>(tailCall): T Returns an proxy object backed by
-
6
Drag image to reposition
-
6
bpf_tail_call特性介绍 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK