Invalid specialized parameter in method lambda list

832 views Asked by At

I am trying to write a simple coin flip program in Common Lisp. This is the code I have

(defun yn
 (let ht (random 1)
  (if (eq ht 1)
   (princ heads)
   (princ tails))
 )
)

It seems simple enough, but I keep getting the error:

"Invalid specialized parameter in method lambda list (LET HT (RANDOM 1) (IF (EQ HT 1) (PRINC HEADS) (PRINC TAILS))): (IF (EQ HT 1) (PRINC HEADS) (PRINC TAILS))"
      )

What could be wrong here?

2

There are 2 answers

2
ycsun On BEST ANSWER

For defun without parameters, there should be an empty parameter list, like the following:

(defun yn ()
  (let ((ht (random 2)))
    (if (eq ht 1)
        (princ heads)
        (princ tails))))
1
Vatine On

Your defun is malformed (lacking an empty parameter list). Your let is malformed (the general structure is (let (<bind-spec>...) <body>), where the binding is either a symbol (initially bound to nil) or a (<symbol> <value>) list.

Both heads and tails seem to be unbound, it's not clear if you mean the literal symbols or are using dynamically-scoped variables.

Your call to random probably doesn't do what you want, the HyperSpec says "Returns a pseudo-random number that is a non-negative number less than limit and of the same type as limit. ", so (random 1) can only return 0.

Below is a cleaned-up version that addresses all of these points, assuming you're after having the literal symbols heads and tails printed.

(defun yn ()
  (let ((ht (random 2)))
    (if (eq ht 1)
      (princ 'heads)
      (princ 'tails)))