MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Single-step evaluation in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81258] Re: Single-step evaluation in Mathematica
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 18 Sep 2007 00:32:38 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fclape$f7b$1@smc.vnet.net>

Szabolcs Horv=C3=A1t wrote:

> I would like to have a function, hasOwnValue, that takes the name of a 
> symbol as a string, and tells whether the symbol has an own-value.  It 
> should do this without evaluating the value of the symbol.
>
> Examples:
>
> abc := Print["side effect"]
> xyz =.
>
> In := hasOwnValue["abc"] (* "side effect" must not be printed *)
> Out = True
>
> In := hasOwnValue["xyz"]
> Out = False
>
> How can I achieve this?

The following function check whether a symbol has any ownvalues defined
and returns True or False, accordingly. Note that we set the attributes
HoldAll to the function so it does not evaluate its argument and one can 
use the regular name of the symbols rather than wrapping them in a
string to prevent evaluation.

In[1]:= hasOwnValueQ[s_Symbol /; OwnValues[s] === {}] = False;
hasOwnValueQ[s_Symbol] = True;

SetAttributes[hasOwnValueQ, HoldAll];

abc := Print["side effect"]
xyz =.
efg = 6;

hasOwnValueQ[abc]
hasOwnValueQ[xyz]
hasOwnValueQ[efg]

Out[7]= True

Out[8]= False

Out[9]= True

--
Jean-Marc



  • Prev by Date: Re: Button Help Example
  • Next by Date: RE: Button Help Example
  • Previous by thread: Re: Single-step evaluation in Mathematica
  • Next by thread: Re: Single-step evaluation in Mathematica