Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.8K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

User Defined Heads

Posted 11 years ago

Is it possible to define one's own Head or type in Mathematica? If so then how does one do it? Thank you.

POSTED BY: Roger Backhus
4 Replies
POSTED BY: Sean Clarke
Posted 11 years ago

Yes, you are correct and function names are heads. I forgot about that. But, I was thinking about my own type like an Interger or Complex. Then I'd create the infrastructure around my type to handle sums, products, ...

POSTED BY: Roger Backhus

Actually you can use the Head-mechanism to build type-like categories very similar to classes in C++. (I did this for various geometrical classes, and for state spaces of (quantum-) mechanical systems.) Once, when one learned Mathematica from Stephen Wolfram's Mathematica book, one found among the Mathematica-supported programming styles also a chapter on the object oriented programming style with instructive examples. I don't know why WolframResearch no longer claims to support object oriented programming. Perhaps there is a general understanding that 'object orientation' implies that there is a mechanism for inheritance. Heads seem not to support inheritance in any direct way. One important way to use user-defined heads is 'function overloading': Assume you have introduced two heads h1, h2. Then you can define

f[x_h1] := ... ; f[x_h2] := ... ;

and when you call f(x) the evaluation will depend on whether x is of form h1[...] or of form h2[...].

POSTED BY: Ulrich Mutze

Yes you can make your own Head.

Bit I'm afraid I don't actually understanding your question as it is written. Are you asking how to make a fuction?

If you have a background with programming already, I would recommend this introduction. http://www.mathprogramming-intro.org/

Here is how you make your own head in Mathematica:

hereIsMyHead[]

You just write it. That's it. There's no grand declaration of its existence or anything. If you want it to do something ... you can program that head to do something. For example, we can program the head to do something to an argument, which basically makes it a function:

hereIsMyHead[a_] := "The Head Called hereisMyHead was given " <> ToString[a] <> " as an argument

The Wolfram Language isn't really typed. As you have noted, heads take the place of types in many respects.

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard