1

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
2
  • 1
    (first numerals), not (first (numerals)). Commented Dec 1, 2015 at 8:37
  • 2
    Probably you get something to work much easier by not writing 'messy' code and by better formatting from the start. Commented Dec 1, 2015 at 9:03

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that was very informative, and helped me solve the problem. Also thank you for the advice on names my background is in c based languages and I started learning lisp 2 days ago, so some of the practices are very different for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.