Whaddaya mean it's not a symbol?

Discussion of Common Lisp
Post Reply
mszegedy
Posts: 2
Joined: Mon May 30, 2011 2:35 pm

Whaddaya mean it's not a symbol?

Post by mszegedy » Mon May 30, 2011 2:57 pm

'K, so I'm a rather amateur Lisper (with years of experience in C), and I'm writing a short, simple text-based RPG based on the popular Latin textbook Ecce Romani. The context isn't important, though.

In my quasiquoted *nodes* variable I have a function that checks whether an event has happened (t is consed to the list after the event in my global variable once it happens):

Code: Select all

(defparameter *evnts* '((cubicula1 ((event2 ())))
                        (horto ((event1 ())))
                        (rivum ((event3 ())))
                        (cirmax ((event4 ()) (event6 ())))
                        (lectus ((event5 ())))))

(defun if-event (locvar num address decrement) ;; Didn't feel like writing this out every time I called it… also writing this occupies my time.
  (if (nth num (cdr (assoc (locvar *evnts*)))) (address) (decrement)))
(go ahead and yell at me about superfluous parentheses; I'm paranoid)

The problem is that in testing this out in clisp, I get an error that the first argument is not a function name, and that I should try using a variable instead (or if I neglect to put an apostrophe in front of it, that it's not a function). I've tried this every which way; I've even tried changing the definitions in the argument of the definition of the function itself. Here's a list of things that don't work:

Code: Select all

(if-event ('horto 0 '(There is now a guy with the letters "F-U-G" branded on his forehead.) '(There is nobody here.)))

(if-event ('(horto) 0 '(There is now a guy with the letters "F-U-G" branded on his forehead.) '(There is nobody here.)))

(if-event (horto 0 '(There is now a guy with the letters "F-U-G" branded on his forehead.) '(There is nobody here.)))

(defun if-event ('locvar num 'address 'decrement) ;; Didn't feel like writing this out every time I called it… also writing this occupies my time.
  (if (nth num (cdr (assoc (locvar *evnts*)))) (address) (decrement)))

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Whaddaya mean it's not a symbol?

Post by ramarren » Mon May 30, 2011 10:18 pm

mszegedy wrote:(go ahead and yell at me about superfluous parentheses; I'm paranoid)
There is no such thing as superfluous parentheses in Lisp. Parentheses are the primary syntax element, and usually all are meaningful. In particular the (symbol ...) syntax is a function call for a function named by the symbol in function context. This means that your expressions like (assoc (locvar *evnts*)) are invalid, because it calls the ASSOC function with one argument which is a result of calling the LOCVAR function on special variable *evnts*. This is wrong, because ASSOC requires two primary arguments and there is probably no such function as LOCVAR, since it seems you meant to refer to the function argument.

Also note that by function context above I mean either a global function, or a function established by FLET/LABELS. To call a function in variable context (passed as argument, for example) you need to use FUNCALL or APPLY.

A good free book about Common Lisp available online for people who already know programming is Practical Common Lisp, you probably should read that, as it explains the syntax and the proper use of QUOTE in more detail.

Post Reply