Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I believe this a question that most of us have encountered when we were beginners but forgot when we got more familiar with Mathematica.

We know the notation $y=y(x)$ for functions, where $y$ here simultaneously represents the functional relation and the dependent variable, isn't rare. We also know it's trouble in Mathematica because it'll cause infinite recursion:

y = y[x]

$RecursionLimit::reclim: Recursion depth of 256 exceeded. >>

Hold[y[x]][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]...

This behavior makes the language strict, of course. But recently, when I found myself in a situation with some justification for using a lot of $y=y(x)$ style notation, the question in the title came to my mind: what if this notation were well behaved in Mathematica?

Needless to say, though quite similar, there're many differences between Mathematica syntax and traditional math notation. For most cases we can easily see the advantages of such deviations. For example, the strict distinctions of [],(),{},[[]]. However, the advantage of the currently built-in behavior of y = y[x] seems not to be obvious. In other words, I can't see what kind of problems will arise if a symbol were allowed to have OwnValues and DownValues at the same time. In such case, OwnValues of a symbol would only be evaluated when the symbol is not the head of any expressions.

Can I achieve this feature in Mathematica?

share|improve this question
3  
You can, but using Stack, which makes it inefficient, and won't work if inside StackInhibit. Otherwise, I think we have to wait until they create the attribute OnlyEvaluateIfNotHead, or create the OnlyIfNotHeadOwnValues –  Rojo Jun 13 at 15:23
1  
Otherwise, you have to get hacky, such as creating a lexical environment. As to the advantages of how it is, there are many, and they will come up more often soon in v10, with all the new "operator versions" of functions like Map, Apply, etc –  Rojo Jun 13 at 15:28
1  
If you care more about close-to-math notation more than debating language features, this would be a solution: 'With[{y = y[x]}, y]'. –  Yi Wang Jun 15 at 7:06
add comment

1 Answer 1

up vote 1 down vote accepted

Your question does not make sense to me since the infinite recursive pattern that troubles you has nothing to do with down-values. It is strictly an effect of the way own-values are evaluated.

Consider

Block[{$RecursionLimit = 20}, y = y[x]]

$RecursionLimit::reclim: Recursion depth of 20 exceeded. >>

Hold[y[x]][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]
Column[{OwnValues @ y, DownValues @ y}]
{HoldPattern[y] :> y[x]}
{}

As you can see, y has no down-values.

It is my belief that only re-coding the Mathematica kernel could fix the problem you discuss. I hope I'm wrong and that someone smarter than me with post a solution. However, this has come up before and no magic solution was conjured up.

share|improve this answer
1  
I think he just means that he would like for y to be evaluated to something, but y[sth] be treated as something different. In other words, have some kind of ownvalues that won't evaluate if the symbol is a head –  Rojo Jun 13 at 16:40
1  
@Rojo. You may be right. But the OP spends considerable time talking about a symbol having down-values and own-values at the same time as if that would somehow fix the problem. It was even mentioned in the title until you edited it :). I want the OP to know down-values are neither part of the problem nor the solution to the question he raises. –  m_goldberg Jun 13 at 22:37
    
Sorry to my late reply. I did intend to wait for an hour after posting this question but was beaten by sandman within 10 minutes 囧. @Rojo Yeah, you're right, thanks for editing the title, also, thanks for m_goldberg's polishing :D –  xzczd Jun 14 at 5:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.