Hacker News new | ask | show | jobs
by lispm 894 days ago
Lisp is at its core an evaluator for expressions. The routine for that is called EVAL.

Mathematica is a computer algebra system at its core and is a rule-based rewrite system for expressions.

An example in Lisp notation:

In a computer algebra system (CAS) one may enter 5a - 2a

  > (- (* 5 a) (* 2 a))
The CAS would answer with:

  (* 3 a)
It has used rules to simplify the expression and uses some default form. It could have printed a + a + a or 3 * a. It sees that it can't further simplify it, because a has no value and thus returns this simplified expression as it is.

In Lisp things are differently. It takes an expression and tries to compute a value:

   > (- (* 5 a) (* 2 a))
The result in Lisp is "Error: unbound variable a". It can't compute a value, because during evaluation it sees that the variable a has no value. Evaluation of the unbound variable a is an error.

Now you could write an expression simplifier in Lisp: let's call it simplify. Lisp has a quote operator, which returns the embedded thing as it is -> it is not evaluated. We can embed an expression inside quote and thus call simplify with that unevaluated expression as an argument.

   > (simplify (quote (- (* 5 a) (* 2 a))))
The result then could be

   (* 3 a)
One then could write a input loop in Lisp

   (loop (print (simplify (read)))
which then would not be a read-eval-print-loop, but a read-simplify-print-loop.

   (defun read-simplify-print-loop ()
     (loop (print (simplify (read))))
This interactive loop would read expressions and print simplified expressions...

Actually something like that has been done with computer algebra systems written in Lisp, like Macsyma/Maxima and Reduce. But they also then switched to infix syntax for input/output to make it easier for humans to enter mathematical expressions.

Peter Norvig gave in his book "Paradigms of AI Programming, Case Studies in Common Lisp" extensive examples how to implement such a thing in Lisp:

https://github.com/norvig/paip-lisp/blob/main/docs/chapter8....

and

https://github.com/norvig/paip-lisp/blob/main/docs/chapter15...

The advantage of the Mathematica language compared to Lisp is that it can compute with expressions via rules out of the box. Additionally Mathematica is so much more than that: it is an environment, a collection of mathematical knowledge, a cloud service, a specific product on can buy/rent, ...

The drawback is that the semantics are murky and Mathematica is a two-language system: the fast internal code (and much of the environment) is written in C++ and the expressive language is on top.

Lisp OTOH is often much more efficiently compiled with clear(er) semantics.

1 comments

> In a computer algebra system (CAS) one may enter 5a - 2a

> > (- (* 5 a) (* 2 a))

> The CAS would answer with:

> (* 2 a)

Not sure I'd want to use such a CAS. Hint: 5-2 != 2. ;-)

Thanks, edited!