Search code examples
lispcommon-lisp

Confusing Lisp syntax with (operator-integer-operator) format


I am new to lisp and I had a question about this LISP syntax:

(defparameter *binary-operators*
  '((+ 1 +) (- 1 -) (* 2 *)
    (x 2 *) (/ 2 %) (^ 3 expt)))

From what I understand, defparameter allows the binary operators variable to be reassigned but I am confused as to how the (+ 1 +), (- 1 -) ... are evaluated. I know in LISP that (+ 4 6) would result in (4 + 6) = 10 but the same logic would result in (1 + +) which does not make sense. What does the above syntax represent?


Solution

  • In Common Lisp,

    (defparameter name initial-value)
    

    (see the manual) introduces a new special (global) variable name with a new value, given by evaluating initial-value.

    So, in the example above, the special variable *binary-operators* is assigned a list of triples, each of them constitued by a symbol, a number, and another symbol. In other words, it assigns some data to a variable, and not, as you were thinking, redefines the syntax of the language.

    Guessing from the values present in the list, this seems a variable that is assigned a list of arithmetic operators, each of them with the priority, and with the equivalent Common Lisp operator/function. Maybe this is a line of some program that maps arithmetic expressions in lisp s-expressions, or something like that.