Message Boards Message Boards

3
|
7641 Views
|
8 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Understanding function evaluation ...

(Apparently I had problems finding a proper title for this post!)

Dear All,

I was playing around with the simple idea of switching evaluation of some functions globally off or on. Please have a look at the following minimal example:

ClearAll[func, validQ, foo]

(* defining some function - with "evaluation switch": *)
func[arg_?validQ] := arg

(* switching evaluation OFF: *)
validQ[_] = False;

(* making a simple setting: *)
foo = func[a];

(* result - as expected: *)
{foo, func[a]}
(* Out:  {func[a],func[a]}  *)

(* switching evaluation ON: *)
validQ[_] = True;

(* `foo` does not evaluate! *)
{foo, func[a]}
(* Out:  {func[a], a}  *)

So my question is: Why on Earth I cannot make foo evaluate. Obviously I am missing here some very basic point ...

Regards and many thanks -- Henrik

POSTED BY: Henrik Schachner
8 Replies

That is exactly the use case for Update although the documentation about it won't give you in depth insights.

Shortly, once evaluated expressions are internally marked as such and won't undergo reevaluation for efficiency purposes. Unless something triggers them, like Update.

validQ[_] = True;    
Update[func];

{foo, func[a]}
{a, a}
POSTED BY: Kuba Podkalicki

Hello Kuba, very interesting, perfect explanation, many thanks!

Best regards -- Henrik

POSTED BY: Henrik Schachner

Dear Sander, Neil, Hans,

many thanks for looking into that problem! Using SetDelayed was my first guess too. @Hans Milton: Yes, I had found that out already, thank you, and this is probably the way to go. But I basically would like to understand, why my approach above is not working ...

Best regards -- Henrik

POSTED BY: Henrik Schachner
Posted 4 years ago
In[1]:= func[arg_ /; validQ] := arg
        foo[a_] := func[a]

In[3]:= validQ = False;
        foo[p]
Out[4]= func[p]

In[5]:= validQ = True;
        foo[p]
Out[6]= p
POSTED BY: Hans Milton

Oops, I guess I should have tried it first!

POSTED BY: Neil Singer

Henrik,

The problem is because of Set[] (=) vs SetDelayed[] (:=)

When you defined foo with =, validQ was false. If you define foo with := it will use the value of validQ at evaluation time, not at define time.

I hope this helps,

Neil

POSTED BY: Neil Singer

I tried that, changing all to SetDelayed, but also didn't work for me…

POSTED BY: Sander Huisman

Interesting question for which I have no answer unfortunately, but I hope to find out by leaving a message here…

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

Group Abstract Group Abstract