I am used to the deprecated elisp macro flet
and I was told to change to cl-flet
. However upon making this change some of my elisp programs stopped working and I realized the reason is that, unlike flet
, cl-flet
does not allow for recursive functions. For example, defining the function below
(defun show-problem-in-action (x)
(cl-flet (
(factorial (n)
(if (= n 0)
1
(* n (factorial (- n 1))) )))
(factorial x) ))
one gets no error by calling
(show-problem-in-action 0)
Output: 1
because the "cl-flet-defined" function factorial
does not call itself when "x=0". However
(show-problem-in-action 5)
produces the error void-function factorial
.
On the other hand, replacing cl-flet
by its deprecated macro flet
, such as below
(defun no-problem-with-deprecated-macro (x)
(flet (
(factorial (n)
(if (= n 0)
1
(* n (factorial (- n 1))) )))
(factorial x) ))
allows for recursive invocation:
(no-problem-with-deprecated-macro 5)
Output: 120
If cl-flet
is not working, what would be my best alternative to replace flet
with, still being able to call functions recursively?