所以我对lisp很新,而不是程序员先进.刚开始真的.
我正在努力从头开始尝试获得一个非常简单的遗传算法,虽然我的大多数代码似乎都按照需要执行,但我仍然遇到这样一个简单的错误/误解,我被阻止从底部...尽管我在网上试图寻找解决方案,但显然我没有得到一些东西.
基本上,我知道它与我试图调用变量的事实有关,就好像它是一个运算符(变量保存),因此它告诉我我的函数(实际上只是一个持有运算符的变量)是没有定义的.
我的代码的开始工作正常.
(defvar *nb* 8)
(defvar *random-operators* '(+ - * /))
(defun get-operator ()
"Returns an element of the list of operators chosen at random."
(nth (random (length *random-operators*)) *random-operators*))
Run Code Online (Sandbox Code Playgroud)
那么(get-operator)
根据需要让我成为四个随机运算符之一.
我使用这个更简单的代码来测试我的代码的结构,它可以按照需要工作.
(defun ga-simple ()
"Returns the value of genome once it matches *nb* and prints
the number of generations required."
(do ((genome (random 10) (random 10))
(generation-counter 1 (incf generation-counter)))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome …
Run Code Online (Sandbox Code Playgroud) common-lisp ×1