Message Boards Message Boards

GROUPS:

New function suggestion: Dont

Posted 3 years ago
3531 Views
|
5 Replies
|
14 Total Likes
|

In the spirit of using WL to talk to AI, this function should be useful, It is like Do except it tells the program what not to do.

A simple example might be

Dont[Outer[Divide, Range[-5,5], Range[-5,5]],  DivideByZero]

There are versions already in the language, like when you solve a differential equation you can stop it under some conditions, and of course there is Break.

One might think this is silly but because of computational irreducibility it is impossible to always predict when something bad will happen.

code 38

5 Replies

Is this why Wolfram Language has no Try clause? Because Do || Don't, !∃Try ?

+1 for the Yoda reference.

But what should it return then for those cases?

Didnt[DivideByZero] ?

Good question Sander. It could be Inactive code taking the place whenever the execution tries code which matches the Don't pattern.

There could be an expression evaluated, like Check which returns something when a message is generated, but evaluate it in place inside the expression. Another analogy is with the optional second argument to Dynamic. which evaluates when a variable changes its value.

In order to give a concrete answer,

Dont[Table[2/i, {i,-2,2}], DivideByZero]

might return

{-1 -1/2,  Inactive[Divide][2,0], 1/2, 1}

Here is an example, creating function from it won't be easy if it is meant to be general, but is certainly possible:

Internal`InheritedBlock[{Divide},
   Unprotect@Divide;
   Divide[x_, 0] := Inactive[Divide][x, 0];
   Outer[Divide, Range[-1, 1], Range[-1, 1]]
 ]

{{1, Inactive[Divide][-1, 0], -1}, {0, Inactive[Divide][0, 0], 0}, {-1, Inactive[Divide][1, 0], 1}}

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