Mathematica vs Lisp Syntax

By Xah Lee. Date: . Last updated: .

Symbolic Expression

here's a example of Mathematica syntax in FullForm. (note the complete nested regularity F[…].)


Note, the only character used are English alphabets, the square brackts [ ], and comma ,. The whole language is just these character, corresponding to lisp's parenthesis and space.

here's the same thing in lisp form. (called symbolic-expression (aka s-exp))

 (vectorAngle (list (Pattern a1 (Blank)) (Pattern a2 (Blank))))
 (let (list x y)
    (setq (list x y)
          (N (* (list a1 a2)
                (exp (sqrt (+ (exp a1 2) (exp a2 2))) -1))))
    (if (eq x 0)
        (if (equal (signum y) 1) (* π (exp 2 -1))
          (* (* -1 π) (exp 2 -1)))
      (if (eq y 0) (if (equal (signum x) 1) 0 π)
        (if (equal (signum y) 1) (acos x)
          (+ (* 2 π) (* -1 (acos x)))))))))

Meta Expression

Following is the same thing in Mathematica InputForm. (which is a syntax layer on top of the regular nested form. Such is known as meta-expression (aka m-exp) in lisp world, conceived by lisp inventor John McCarthy, but never caught on in lisp comunity.)

vectorAngle[{a1_, a2_}] := Module[{x, y},
    {x, y} = {a1, a2}/Sqrt[a1^2 + a2^2] // N;
    If[x == 0, If[Sign@y === 1, π/2, -π/2],
      If[y == 0, If[Sign@x === 1, 0, π],
        If[Sign@y === 1, ArcCos@x, 2 π - ArcCos@x]]]]

Note the vectorAngle[{a1_, a2_}] := …? That's your lisp macros, but in a advanced form, called pattern matching. The “a1_” and “a2_” are patterns, and the right-hand-side is the transformation spec. [see Intro to Mathematica Pattern Matching for Lisp Programers]

StandardForm (rendered display)

Following is the same thing rendered in Mathematica StandardForm.

Mathematica syntax StandardForm screenshot
Expression rendered in Mathematica StandardForm.

for explanations, see Concepts and Confusions of Prefix, Infix, Postfix and Lisp Notations

Ask me question on patreon