Scheme’s define in Common Lisp

  softwareengineering

In Common Lisp, we have to use the let form to declare a new lexically-scoped variable. This means that the code either looks like that written in C89 (all variables declared on top of scope), or acquires unreadably deep nesting. let* is somewhat useful, but is not always applicable.

Scheme ‘solves’ this problem by having a define form, that allows a lexical variable to be declared without creating a new nesting level.

So, the question is, is it possible to have Scheme-like variable declarations in Common Lisp, that do not increase the nesting level?

4

You don’t have to use LET when using DEFUN: you can define local variables in the parameter list:

(defun foo (a)
  (let ((b (expt a 3)))
    (+ a b)))

is also

(defun foo (a &aux (b (expt a 3)))
  (+ a b))

That’s not often seen in code, but it is a standard feature of Common Lisp.

Notice also that the syntax of LET and DEFUN is different in CL from what you are used to in Scheme: CL:LET allows declarations before the Lisp forms. CL:DEFUN allows documentation and declarations before the Lisp forms.

Note also that for example in Scheme R7RS small all internal DEFINE forms need to appear at the top of the enclosing form. Some Scheme implementations allow these forms to appear later in the body, but that is not in the R7RS small standard.

Disclaimer: This answer was written only using natural intelligence.

1

LEAVE A COMMENT