Search code examples
lispelisp

flet equivalent of let*?


There is not an flet* for flet as there is let* for let that I can find in emacs lisp -- so are any of these four alternatives more idiomatic for defining a function used multiple times by a single function defined by flet? In this example, add1 is the function that I want to reused within add1twice but not used outside of it.

Option 1

(defun add2 (x)
  (flet ((add1 (x) (1+ x)))
    (flet ((add1twice (x)
              (add1 (add1 x))))
      (add1twice x))))

Option 2

(defun add2 (x) 
  (flet ((add1twice (x)
            (flet ((add1 (x) (1+ x)))
              (add1 (add1 x)))))
     (add1twice x)))

Option 3

(defun add2 (x)
  (flet ((add1twice (x)
            (let (add1)
              (fset 'add1 (lambda (x) (1+ x)))
              (add1 (add1 x)))))
      (add1twice x)))

Option 4

(labels ((add1 (x) (1+ x)))
  (defun add2 (x) 
    (flet ((add1twice (x)
              (add1 (add1 x))))
      (add1twice x))))

(All of these produce the same results.)


Solution

  • Though not too clear in the documentation for flet, its macroexpansion reveals that it tolerates sequential binding:

    (defun add2 (x)
      (flet ((add1 (x) (1+ x))
             (add1twice (x)
               (add1 (add1 x))))
        (add1twice x)))
    

    The documentation mentions establishing "let-style bindings"—not let*—but the definition above works nonetheless.