Re: Lisp's QUOTE and Mathematica's "Hold"
- From: Jon Harrop <usenet@xxxxxxxxxxxxxx>
- Date: Sun, 21 Aug 2005 05:21:59 +0100
Peter Seibel wrote:
> Jon Harrop <usenet@xxxxxxxxxxxxxx> writes:
>> You can see Hold in Mathematica but not QUOTE in Lisp but isn't that more
>> a function of the pretty printer rather than the evaluation?
>
> No. At least in Lisp case, the QUOTE form was evaluated and it
> evaluated to the quoted expression. The pretty printer has no way of
> knowing whether a form came from a QUOTE expression or was constructed
> in some other way:
>
> '(+ 1 2) ==> (+ 1 2)
Right, but Hold[1+2] is Mathematica's equivalent of the quoted expression,
i.e. an expression held in unevaluated form.
> where ==> means "evaluates to". But so does:
>
> (list '+ 1 2) ==> (+ 1 2)
>
> and also:
>
> (cons '+ (cons 1 (cons 2 nil))) ==> (+ 1 2)
In Mathematica:
In[20]:= Hold[{1, 2}] /. {List -> Plus}
Out[20]= Hold[1 + 2]
The latter "quoted" expression is indistinguishable from the
directly-created one, just like the Lisp.
> Given any of those (+ 1 2) lists, you can't tell how it was
> produced. And if you then evaluate the list you get 3:
>
> (eval '(+ 1 2)) ==> 3
> (eval (list '+ 1 2)) ==> 3
> (eval (cons '+ (cons 1 (cons 2 nil)))) ==> 3
Yes, this is also the same:
In[21]:= Release[Hold[1 + 2]]
Out[21]= 3
In[22]:= Release[Hold[{1, 2}] /. {List -> Plus}]
Out[22]= 3
As Nathan Baum just explained, you can tell between different instantiations
of the same quoted expression in Lisp, whereas you cannot in Mathematica.
> By contrast (at least according to what others have said in this
> thread), in Mathematica Hold "evaluates" to something that carries
> it's "heldness" with it until someone explicitly Releases it. Thus you
> have:
>
> Hold[whatever] ==> Hold[whatever]
>
> and assuming for the sake of argument there is an EVAL that does the
> same thing as implied by the ==> above:
>
> EVAL[Hold[whatever]] ==> Hold[whatever]
Yes, I think its "Evaluate" rather than EVAL. In Mathematica,
Hold[Evaluate[expr]] evaluates expr and then holds it, IIRC.
> while
>
> Release[Hold[whatever]] ==> whatever
>
> but Release (again, as I understand it) is not a general purpose
> evaluator--all it does is strip off a level of heldness (or maybe all
> levels of heldness?)
Exactly (the former). This is where the similarity begins to breakdown.
However, despite the difference in method of evaluation, the results are
the same.
> Of course I could be all wrong about Mathematica
> having never used it; I'm just going by the other descriptions in this
> thread.
I think you've got the right idea. Mathematica then repeatedly evaluates the
resulting expression until it stops changing. This might seem like a
massive departure from other languages, including Lisp, but actually it is
virtually the same thing.
In Mathematica, if the resulting expression is an integer then it won't
change so evaluation stops.
If the resulting expression is a call to a recursive function then that
function will be invoked and the repeated evaluation by Mathematica simply
corresponds to the recursive function calls in most other languages.
So, term-level interpreters for Mathematica and Lisp are very similar.
--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com
.
- Follow-Ups:
- Re: Lisp's QUOTE and Mathematica's "Hold"
- From: Nathan Baum
- Re: Lisp's QUOTE and Mathematica's "Hold"
- References:
- Very poor Lisp performance
- From: Jon Harrop
- Re: Very poor Lisp performance
- From: Jon Harrop
- Re: Very poor Lisp performance
- From: Ulrich Hobelmann
- Re: Very poor Lisp performance
- From: Jon Harrop
- Re: Very poor Lisp performance
- From: Brian Downing
- Re: Lisp's QUOTE and Mathematica's "Hold"
- From: Jon Harrop
- Re: Lisp's QUOTE and Mathematica's "Hold"
- From: Peter Seibel
- Very poor Lisp performance
- Prev by Date: Re: Very poor Lisp performance
- Next by Date: Re: Lisp's QUOTE and Mathematica's "Hold"
- Previous by thread: Re: Lisp's QUOTE and Mathematica's "Hold"
- Next by thread: Re: Lisp's QUOTE and Mathematica's "Hold"
- Index(es):
Relevant Pages
|