5 ms·
Mathematica has lisp essentially as a subset built on top of its rewrite system. In other words its rewrite system is so powerful that lisp can be naturally thr
by tokipin 11y ago
Mathematica has lisp essentially as a subset built on top of its rewrite system. In other words its rewrite system is so powerful that lisp can be naturally threaded into it.
In particular the symbolic nature of the language means that everything is first class, functions and function constructions ("functional programming") and expressions in general ("macros") being just basic cases.
- gohrt 11y agoCan the user define a macro in Mathematica language?
- evanb 11y agoIt's not quite so clear as that. In Mma you can give special meanings to any patterns of symbols you like, which is very much like defining a macro.
- taliesinb 11y agoThere aren't read-time macros (yet). I'm actually in the process of designing a new package system that solves this (among many other problems).
- tokipin 11y agothe concept of "macros" in Mathematica doesn't really exist as its own concept because the whole system is based on rewriting structure. for example, the ReplaceAll function[1] which is commonly used in idiomatic code is basically an in-place macro definition and execution, yet it wouldn't occur to most people that it's anything special. if you're trying to make a traditional macro, you would implement it as a function with the HoldFirst et al attribute. an important thing to know is that ReplaceAll uses arbitrary structural patterns. it's not a simple "turn every a into b". you can say things like "turn every application of the symbol F on an even number into an application of G on that number divided by two". expr /. F[n_?EvenQ] :> G[n/2] [1] https://reference.wolfram.com/language/ref/ReplaceAll.html https://reference.wolfram.com/language/ref/ReplaceAll.html
- lispm 11y ago> Mathematica has lisp essentially as a subset built on top of its rewrite system. Right. > In other words its rewrite system is so powerful that lisp can be naturally threaded into it. a) lots of systems (turing machines, ...) can implement Lisp. b) it's not that natural in Mathematica. The natural part comes from its symbolic nature. But if you look at the documentation and read about how they implement lexical closures, it's all vague and not that pretty. The implementation actually needs to rewrite variable names, IIRC. I wonder why their documentation is so vague? They don't want independent implementations of their language?
- taliesinb 11y ago> b) it's not that natural in Mathematica. The natural part comes from its symbolic nature. But if you look at the documentation and read about how they implement lexical closures, it's all vague and not that pretty. The implementation actually needs to rewrite variable names, IIRC. That's indeed what Module does, and it is indeed ugly and slow. When we add parse-time macros it'll be trivial to fix this. For now, many people use Block instead, which does dynamic scoping and is a bit cheaper. I actually really like Block, because it gives you the convenience of a couple of globals that you don't require you to keep passing them along among your backend functions, but they're properly bound to the lifetime of your function (and are re-entrant etc).
- tokipin 11y agoi think you're thinking that Lispification is some kind of goal for the language, but it's not. the lispininess is just one aspect of the language, which is why i think calling it a lisp isn't fair to Mathematica -- it's more powerful/abstract/general than that (in particular because everything is essentially quoted by default - though in their raw forms i suppose both lisp and Mathematica are essentially equivalently capable). the precise character of the lispy aspects of the language (eg the language not "really" having safe-locked closures) is imo just an implementation detail.
- lispm 11y agoMacsyma/Maxima is a computer algebra system written in Lisp, full 20 years older than Mathematica. It implements similar evaluation capabilities. The book Paradigms of AI Programming by Peter Norvig shows how to implement these evaluation strategies in efficient Common Lisp.