在LISP

时间:2015-12-01 08:28:21

标签: lisp common-lisp

作为一个项目,我需要使用递归在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

1 个答案:

答案 0 :(得分:5)

在Common Lisp中,形式(blah)表示“调用函数blah”,形式(blah foobar)表示“调用函数foo,参数foobar”。因此,当你真的想要使用变量的值时,你告诉编译器在多个地方调用函数numerals

此外,除非您的lisp环境使用“现代模式”,否则"toNatural"表示的符号与"tonatural""TONATURAL"表示的符号相同,请勿使用用例区分单词分隔符,使用“ - ”(所以(defun to-natural ...)。