DEFUN
The most basic mistake in your code is that DEFUN
is not correct for nested functions. DEFUN
is a top-level macro, defining a top-level function and should be used in top-level forms.
Nested functions are defined in Common Lisp with FLET
and LABELS
. LABELS
is used for recursive sub-functions.
Naming
Symbols like FooBarBaz
are not use in Common Lisp. By default Common Lisp upcases all names internally, so the case information gets lost.
Usually we write foo-bar-baz
.
Checking symbols
Use CASE
(or ECASE
) instead of (cond ((equal foo 'bar) ...))
.
Architecture
Usually I would write that piece of code using CLOS, the Common Lisp Object System.
In a more functional style I would propose the following:
-
use
LABELS
for the local procedures. -
set the state to the next function. A function is written as
(function my-function)
or#'my-function
. -
the controller just calls the next function from the state variable. It is not necessary to list all cases.
manpreet
Best Answer
2 years ago
I have been making this FSM today. However, as this is probably the biggest practical program I have ever written in CL, I don't know if there are some things that could be improved, or if using a closure is the best thing here.
Any feedback is appreciated.
EDIT
I have applied all the suggested changes but for the flet/labels one. Everything worked fine until I changed the one of "set the state to the next function". Now, the macro
enter
doesn't seem to be ever called.This is the current state of the code, with the required code to make it work