Re: rightTree[tree[_, _, right_]] := right Hu?
- To: mathgroup at smc.vnet.net
- Subject: [mg38028] Re: [mg37993] rightTree[tree[_, _, right_]] := right Hu?
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Tue, 26 Nov 2002 00:49:25 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On Monday, November 25, 2002, at 01:56 AM, Steven T. Hatton wrote: > I found this: > rightTree[tree[_, _, right_]] := right > > in Dr. M¡î¡×der's Computer Science with Mathematica. I understand the > spirit of > the statement, but it looks rather strange to me. Is there a way of > describing, in a language such as English, exactly what this definition > means? The part that I find confusing is the inclusion of what looks > like a > function call in the parameter list. This simply defies my sense of > how > programming languages work. That doesn't mean it's a bad thing. It > just > means that I don't feel comfortable with the syntax. > It's best to forget about function "calls", think about expression evaluation. What I mean is Mathematica operates using a read-eval-print loop similarly to LISP (again, it really helps to have some exposure to LISP and Prolog when dealing with programming issues in Mathematica). So you enter an expression, Mathematica reads it, then passes it off to be evaluated then finally prints the resulting expression. Function call implies all sorts of arcana about putting references or copies of arguments on a call stack, jumping to a subroutine, restoring the call stack etc. that really isn't a factor in Mathematica. What does this have to do with the above expression? First let's get rid of the infix notation rightTree[tree[_,_,right_]]:=right is equivalent to SetDelayed[rightTree[tree[_,_,right_]],right] when the evaluator sees the SetDelayed it means means create a rule associating the DownValue of the first argument (held) with the second (also held). Since all the arguments are held there is no more evaluation. That's pretty simple, the real magic happens when you evaluate the expression rightTree[(some expression with head tree and three arguments, let's call it t for now)]. The evaluator searches the rule list of rightTree for the most specific match, since t has head tree (and assuming no more specific rule has been written) that's the rule that transforms the expression. That rule says that the third argument of the expression tree[_,_,_] is to be called right and that is the value of the expression rightTree[tree[_,_,right_]]. So the expression represented by the pattern right_ is passed to the evaluator. If that expression has no transformation rules associated with it then evaluation stops and that value is returned. This isn't really a question of syntax (since the LISP syntax looks something like (defun rightTree (tree) (car (nthcdr 2 tree))), I've omitted the type checking) it's a question of semantics. Incidentally I believe the Prolog syntax actually looks a lot more like the Mathematica, it's been a while since I dealt with Prolog but I think rightTree is isaTree(Node) :- atom(Node) isaTree([Left,Node,Right]) :- isatree(Left),atom(Node),isatree(Right). rightTree(tree) :- isaTree(tree),Right. This version has the type checking but a real Prolog programmer would probably use guards. > Is there somewhere in the Mathematica Book , or Help which discusses > such > arcana? > This is all explained in section 2.5.4 of the Mathematica book. The key is to understand that at the most basic level all Mathematica does is take expressions, match them to rules (as expressed by patterns), apply the transformation and repeat the above process until no more transformations can be done. Regards, Ssezi