programming in Mathmatica
- To: mathgroup at smc.vnet.net
- Subject: [mg13422] programming in Mathmatica
- From: john at dlugosz.com (John M. Dlugosz)
- Date: Thu, 23 Jul 1998 03:33:18 -0400
- Abuse-reports-to: abuse at airmail.net to report improper postings
- Organization: at home
- Sender: owner-wri-mathgroup at wolfram.com
Consider this function:
(*1*) ackPrim[m_Integer, 0] := (
(*2*) ++counter;
(*3*) {result_,rc_ } -> cAck[m-1,1]; (*4*) { result, rc+1 }
(*5*) );
The function cAck returns a list of two items. In Perl I write:
my ($result, $rc) = cAck ($m-1, 1);
that is, the first item in the list gets assigned to result, the second
to rc. I could get a list and then refer to list[[1]] and list[[2]],
but I was attempting to use the Blank and pattern matching to
"deconstruct" the list succinctly, as alluded to in the Big Book.
This is an idiom I recognise from Prolog, and to some degree in Perl.
Of course, the above code does not work, because result and rc are
localized to that one line.
=> So what is a succinct idiom for this in Mathematica?
Next, you see I'm using a list of two values. For more complex
programs, such "structures" represented as lists would get a little
awkward. I can imagine using access functions to pull the correct
member of a list, so I get the benifit of named structure members, and
even creating an operator for that which works similarly to the "dot"
in Algol-decendent languages. So, I assume people have done this
already and there is a standard idiom in circulation for it. What is
it, so I don't have to re-invent my own?
=> What is the standard practice for using what other languages do with
"structures"?
--John