【发布时间】:2016-03-05 02:31:35
【问题描述】:
作为一个项目,我需要使用递归在 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
【问题讨论】:
-
(第一个数字),而不是(第一个(数字))。
-
通过不编写“凌乱”的代码并从一开始就更好地格式化,您可能会更轻松地工作。
标签: lisp common-lisp