views:

103

answers:

1

Hi, I'm trying to have a function compare the first argument of a passed in argument to a value, then if it is true, perform some function, then recursively call the same function.

(defun function (expression)
  (cond
    ((equal (first expression) "+")
     (progn (print "addition")
            (function (rest expression))))))

For some reason, though, it is only going through it recursively and not printing. Thanks.

A: 

Perhaps you mean to say:

(defun function (expression)
  (cond (expression
         (cond (equal (first expression) "+")
               (print "addition")))
         (function (rest expression)))))

the original recurses only if (first expression) is "+" and also fails to do a nil-check.

themis
After reformatting you can immediately see that this will not work. The error will be that the variable "equal" is not bound.
Svante
You're right, I dropped the "()" around the "(equal ...)". Fingers were faster than brain.
themis