Problem with cons function
Well, I started recently to learn lisp, and doing a small program I've found a little problem. The problem is to write a function that adds a title to a name if it doesn't already have one. My code is:
(setf *man-names* '(carlos pablo dani sergio))
(setf *woman-names* '(eva alba luna laura开发者_如何学JAVA))
(defun titledp (name)
(cond ((member (car name) *man-names*) nil)
((member (car name) *woman-names*) nil)
(t t)))
(defun add-title (name)
(cond ((member (car name) *man-names*) (cons 'Mr. name))
((member (car name) *woman-names*) (cons 'Mrs. name))))
(defun title (name)
(cond ((titledp (name)) name)
(t add-title (name))))
When, in 'add-title', cons is called I get a problem that says the function 'name' isn't defined. Why does that happen? how can I fix it?
Thank you :)
Your parentheses are funny. In your title
function, you use (name)
a couple of times. That means to call the function called name
with no arguments. I think this is what you want:
(defun title (name)
(cond ((titledp name) name)
(t (add-title name))))
Since you are expecting a list for your name value, you need to submit a list. Thus, (add-title '(carlos)) or (add-title (list 'carlos)) will work.
精彩评论