1
$\begingroup$

I'm figuring how to implement a function behavior based on the Head of its argument.

I mean

Unprotect@Head;
Head[p_[x]]:= Kind;
Protect@Head;
function[h_]:= 0;
function[p_Kind]:= 1;

Head[f[x]]
function[f[x]]
function[f]

the first instance of function should evaluate to 1 whereas the second instance should evaluate to 0. However, both evaluate to 0. Notice that Head[f[x]] evaluate to Kind as expected.

Where am I doing mistake(s)?

Thank you very much

$\endgroup$
  • $\begingroup$ Try function[p_ /; Head@p === Kind] := 1; $\endgroup$ – ciao Mar 21 '14 at 8:10
  • $\begingroup$ @rasher Thank you now it works. $\endgroup$ – Fabio Mar 21 '14 at 8:19
  • $\begingroup$ @kuba your solution failed though I expected it works too... strange $\endgroup$ – Fabio Mar 21 '14 at 8:20
  • 1
    $\begingroup$ Yes, but pay heed to Kuba's comment - in general, doing things requiring Unprotect on system stuff is either not required, a bad idea, or both. There are times when that's the only viable way, but there's usually a better/cleaner/safer way. $\endgroup$ – ciao Mar 21 '14 at 8:21
  • $\begingroup$ I didn't say it will work. This was about Unprotecting important things ;) I've deleted my comment because I find myself a newbie in internals so let's wait for someone competent. @Rasher's solution works but I think it's not how one may want to do this. $\endgroup$ – Kuba Mar 21 '14 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.