7
$\begingroup$

My question is in two parts. First, I want to know why

Check[#, "Failed"] &@{0^0, 1^1}

evaluates to

{Indeterminate, 1}

while

{Check[0^0, "Failed"], Check[1^1, "Failed"]}

evaluates to

{"Failed", 1}

Then, I'd like to know if there is a way to make an expression of the first type (i.e. having Check[#, "Failed"] with an existing list "applied to it") evaluate to what I would call the "expected result" of

{"Failed", 1}
$\endgroup$

1 Answer 1

7
$\begingroup$

When you write Check[#, "Failed"] & you are creating a pure Function, which by default does not hold its arguments in the way that Check does.

So when you evaluate

Check[#, "Failed"] &@{0^0, 1^1}

the list {0^0, 1^1} evaluates first before even being fed to Check. You could create a pure Function that does hold its arguments with something like

In[13]:= Function[x, Check[x, "Failed"], {HoldAll, Listable}]@{0^0, 1^1}

During evaluation of In[13]:= Power::indet: Indeterminate expression 0^0 encountered.

Out[13]= {"Failed", 1}
$\endgroup$
2
  • $\begingroup$ I accepted the answer because the key trick I was missing was the ability to "Hold" an expression. However, I actually waned the return to be Failed or not for each element in the list which I see I didn't make clear originally with &@ instead of &/@ syntax. The following works Check[ReleaseHold[#], "Failed"] & /@ {0^0 // Hold, 1^1 // Hold} $\endgroup$
    – nightshade
    Commented Jan 2, 2023 at 22:16
  • 2
    $\begingroup$ @Nic - see the update which uses the Listable attribute $\endgroup$
    – Jason B.
    Commented Jan 2, 2023 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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