Mathematica vs Lisp Syntax

,

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

SetDelayed[vectorAngle[List[Pattern[a1,Blank[]],Pattern[a2,Blank[]]]],
    Module[List[x,y],
      CompoundExpression[
        Set[List[x,y],
          N[Times[List[a1,a2],
              Power[Sqrt[Plus[Power[a1,2],Power[a2,2]]],-1]]]],
        If[Equal[x,0],
          If[SameQ[Sign[y],1],Times[π,Power[2,-1]],
            Times[Times[-1,π],Power[2,-1]]],
          If[Equal[y,0],If[SameQ[Sign[x],1],0,π],
            If[SameQ[Sign[y],1],ArcCos[x],
              Plus[Times[2,π],Times[-1,ArcCos[x]]]]]]]]]

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

(SetDelayed
 (vectorAngle (list (Pattern a1 (Blank)) (Pattern a2 (Blank))))
 (let (list x y)
   (progn
    (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)))))))))

here's the same thing in Mathematica InputForm. (which is a syntax layer on top of the regular nested form. Such is known as m-exp in lisp world)

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. 〔☛ Intro to Mathematica Pattern Matching for Lisp Programers

following is the same thing rendered in Mathematica StandardForm.

Mathematica syntax StandardForm screenshot
Expression rendered in Mathematica StandardForm.

for explanations, see Concepts & Confusions of {Prefix, Infix, Postfix, Fully Nested} Notations

Mathematica 2 logo
Mathematica 2 logo. 〔☛ Mathematica Logos thru its History
blog comments powered by Disqus