Search code examples
lispcommon-lispclisp

Lisp- "[Function name] Is not a Number"


So I have this simple expression to evaluate and list the two roots of a quadratic equation:

(defun QUADRATIC (A B C) (list (/  (+  (- B) (sqrt(- (* B B) - (* 4 A C)))) (* 2 A)) (/  (- (- B) (sqrt(- (* B B) - (* 4 A C)))) (* 2 A))))

But when I evaluate it in CLISP with any three numbers for parameters, say

(quadratic 2 2 2)

I get the following error: (quadratic 2 2 2) is not a number

I am sure there is an easy fix but I can't figure it out!


Solution

  • There is a syntactic error in your definition (and using an editor that format properly the code help in finding this kind of errors).

    The correct definition is:

     (defun quadratic (A B C) 
        (list (/  (+ (- B) (sqrt(- (* B B) (* 4 A C)))) 
                  (* 2 A)) 
              (/  (- (- B) (sqrt(- (* B B) (* 4 A C)))) 
                  (* 2 A))))
    

    while you have an extra - in the sqrt call: (sqrt(- (* B B) - (* 4 A C)))) (* 2 A)) (the second -).

    The reason for the particular error message is that - used not in function position is a special variable that refer to the current form (see the specification).