作为一个项目,我需要使用递归在 lisp 中制作一个罗马数字转换器。在处理罗马数字到英文部分时,我遇到了一个问题,编译器告诉我我的一个变量是未定义的函数。我是 lisp 的新手,可以使用该程序的任何提示或技巧。我想知道我必须进行哪些更改才能停止出现该错误,如果有人对我的递归有任何提示,我将不胜感激。
我知道我的代码很乱,但我计划在我有可用的代码时学习所有正确的格式化方法。该函数应该采用罗马数字列表,然后将列表的第一个和第二个元素转换为相应的整数并将它们相加。它递归地被调用直到它遇到 NIL 时它会返回一个 0 并添加所有剩余的整数并将其显示为一个原子。希望这是有道理的。提前谢谢你。
(defun toNatural (numerals)
"take a list of roman numerals and process them into a natural number"
(cond ((eql numerals NIL) 0)
((< (romans (first (numerals)))
(romans (second (numerals))))
(+ (- (romans (first (numerals))))
(toNatural (cdr (numerals)))))
(t
(+ (romans (first (numerals)))
(toNatural (cdr (numerals)))))))
(defun romans (numer)
"take a numeral and translate it to its integer value and return it"
(cond((eql numer '(M)) 1000)
((eql numer '(D)) 500)
((eql numer '(C)) 100)
((eql numer '(L)) 50)
((eql numer '(X)) 10)
((eql numer '(V)) 5)
((eql numer '(I)) 1)
(t 0)))
这里是错误。我在这个项目中使用 emacs 和 clisp。
The following functions were used but not defined:
NUMERALS
0 errors, 0 warnings
最佳答案
在 Common Lisp 中,形式 (blah)
表示“调用函数 blah”,形式 (blah foobar)
表示“调用函数 foo,带有参数foobar”。因此,当您实际上只想使用变量的值时,您告诉编译器在多个地方调用函数 numerals
。
此外,除非你有一个使用“现代模式”的 lisp 环境,否则 “toNatural”
表示的符号与 “tonatural”
表示的符号相同或者"TONATURAL"
,不要用大小写来区分断字,用"-"(所以(defun to-natural ...
)。
关于lisp - 变量作为 LISP 中的未定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34015821/