LISP vs WolframLang
The major influence of WolframLang is LISP and APL. [see LISP/APL Root in Mathematica / Wolfram Language]
WolframLang has all the characteristics of LISP:
- Identifiers are Symbols
- Everything is a Expression
- A expression is either a Atomic Expression (number, string, symbol) or a list.
- Purely nested syntax
- Predominantly functional programing
- Reader macro (meta-expression) (lisp
[] ' ` , # ,@ .
vs WolframLang syntax shortcuts ) - Lisp macros (WolframLang symbolic Pattern Matching)
Here's a table of comparison of LISP and WolframLang, for easy learning WolframLang.
lisp | Wolfram Lang |
---|---|
(f a b c)
|
f[a, b, c]
|
; comment
|
(* comment *)
|
lisp | Wolfram Lang |
---|---|
print
|
lisp | Wolfram Lang |
---|---|
+
|
Plus |
-
|
Minus |
*
|
Times |
/
|
Divide |
%
|
Mod |
expt
|
Power |
floor
|
Floor |
ceiling
|
Ceiling |
round
|
Round |
lisp | Wolfram Lang |
---|---|
t |
True |
nil
|
False Null |
and
|
And |
or
|
Or |
not
|
Not |
>
|
Greater |
>=
|
GreaterEqual |
<
|
Less |
<=
|
LessEqual |
= , eq , equal , string-equal
|
Equal ,
SameQ
WolframLang: Equality Test
|
lisp | Wolfram Lang |
---|---|
set , setq
|
Set
WolframLang: Set and SetDelayed
|
lisp | Wolfram Lang |
---|---|
let , let*
|
Module ,
Block ,
With .
WolframLang: Local Variable
|
lisp | Wolfram Lang |
---|---|
if
|
If
WolframLang: If Then Else (Conditionals, Branching)
|
cond
|
Which ,
Switch .
WolframLang: Which, Switch
|
throw
catch
|
Throw Catch |
error
|
Abort |
lisp | Wolfram Lang |
---|---|
while
|
While
WolframLang: Loop
|
mapcar
|
Map
WolframLang: Map Function to List
|
mapc
|
Scan
|
dotimes
|
Do
|
lisp | Wolfram Lang |
---|---|
cons
|
there's no cons in WolframLang. use List[a,b] if you want.
WolframLang List is LISP's vector .
|
list , vector
|
List
WolframLang: List
|
association list, hash table | WolframLang: Association (Key Value Pairs) |
lisp | Wolfram Lang |
---|---|
defun , lambda
|
Function
WolframLang: Define Function
|
defmacro
|
SetDelayed
WolframLang: Set and SetDelayed
|
funcall , apply
|
Apply Apply |
lisp | Wolfram Lang |
---|---|
load
|
Get |
require
|
Needs |
autoload
|
DeclarePackage |
lisp | Wolfram language |
---|---|
progn
|
CompoundExpression
WolframLang: CompoundExpression and Semicolon
|
lisp | Wolfram language |
---|---|
quote or ' `
|
Hold HoldComplete |
“unquote” ,
|
Evaluate |
,@
|
Splice |
now, all those LISP quote, backtick, dot, comma, comma at, are implemented by what's called “reader macros”.
a “Reader”, exists in all LISPs, in emacs lisp, common lisp, scheme lisp, clojure lisp. (also in WolframLang, called Read). Reader is part of lisp compiler that is called as the first thing, to transform what user typed into a lisp list syntax, or “real lisp syntax”.
Now in every language, what you typed, valid source code, is called the lang's syntax. In lisp, the lisp fanatical idiots, call lisp source code as “surface syntax”. After the lisp reader did the syntactical transformation, the lisp fanatics calls that the real lisp syntax. This lisp idiocy generates lots of confusion.
So, for example, this lisp code
'((a . b))
is called “surface syntax” by lisp fanatics.
now after lisp reader did its job, it became
(quote (cons a b))
.
lisp fanatic say this is real lisp syntax, which are lists.
so but why is lisp no have some functional form for some of the “syntactic shortcuts” such as backtick or # or comma or comma at?
well, simply put, bad design. Again, the lisp reader transform them, and if the end result has the form
(f x y z etc)
, then it's fine, lisp fanatics see no need to have a
(f x y z etc)
to stand for the short syntaxes.
this problem, gets deep. it is very hard to understand the effect of quote, etc or see exactly which are syntactical equivalences. The whole thing, is part of the complexity of evaluation.
by the way, the complexity of evaluation of WolframLang is not better. You have Hold, HoldForm, ReleaseHold, HoldAll, HoldAllComplete, Evaluate, Unevaluated, Activate, Inactivate, etc.
Evaluation
so far, we have not mentioned lisp macros.
lisp macro is a lang feature that allow user to write a
syntactical transformation rule.
builtin macros (such as if
, while
), are called special forms in lisp.
extra link Fundamental Problems of Lisp
in WolframLang, the whole language, entire evaluation strategy, can be considered as lisp macros. More properly called, term rewriting system.
In essence, how WolframLang works, is that source code is just a sequence of Symbols and operators. Evaluation is done by just continuously doing syntactical transformation, to end up with a different sequence of symbols, which we take as the result.
The syntactical transformation, follow by “transformation rules”, either builtin, or user defined. (this part is analogous to lisp macros). this syntactical transformation continues until no more rule applies. (that is, no pattern matches)
by the way, the way this works is that assignment returns a value.
a = b
returs b.
and, = has higher stickiness than := , so
f[x_] := f[x] = expensive[x]
is
f[x_] := (f[x] = expensive[x])
then, more specific pattern matching transformation is done before more general one.
so, if WolframLang sees
f[3]
, and if there's already a rule for
f[3]
, it becomes value of
expensive[x]
right away. But if there is no rule for
f[3]
, the delayed transformation rule
f[x_]
kicks in, so it get replaced by
f[x] = expensive[x]
it's important to note,
a = b
FullForm is
Set[a,b]
.
Which means, create a rule, so whever a
occure, replace it by b
.
and
a := b
FullForm is
SetDelayed[a,b]
.
Which means, create a rule, so whever a
occur, replace it by b
, but eval b
only at the time of replacement.