1
votes

As a project I am required to make a roman numeral converter in lisp using recursion. While working on the roman numeral to English portion I am running into a problem where the compiler is telling me that that one of my variables is is an undefined function. I am very new to lisp and could use any tips or tricks possible for this program. I would like to know the changes I would have to make to stop getting that error and if anyone has tips for my recursion that would be appreciated.

I know my code is messy but I plan on learning all the proper ways to format when I have something that works. The function is supposed to take a list of roman numerals and then convert the first and second element of the list into the corresponding integers and add them. It recursively is called until it hits NIL when it will return a 0 and add all the remaining integers and display that as an atom. Hopefully that makes sense. Thank you in advance.

(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)))

here is the error. I use emacs and clisp for this project.

The following functions were used but not defined:
 NUMERALS
0 errors, 0 warnings
1
(first numerals), not (first (numerals)). - uselpa
Probably you get something to work much easier by not writing 'messy' code and by better formatting from the start. - Rainer Joswig

1 Answers

5
votes

In Common Lisp, the form (blah) means "call the function blah" and the form (blah foobar) means "call the function foo, with the argument foobar". Thus, you are telling the compiler to call the function numerals in multiple places, when you actually want to just use the value of the variable.

Also, unless you have a lisp environment that uses "modern mode", the symbol denoted by "toNatural" is the same as the one denoted by "tonatural" or "TONATURAL", don't use case to distinguish word breaks, use "-" (so (defun to-natural ...).